Skip to content

Instantly share code, notes, and snippets.

@mkatsura
Last active December 22, 2015 06:48
Show Gist options
  • Save mkatsura/6433184 to your computer and use it in GitHub Desktop.
Save mkatsura/6433184 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#\u3000Gif\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3 #\n",
"\u6700\u65b0\u306ematplotlib(1.3.0)\u3067\u306fanimation\u306bImagemagick\u3068\u3044\u3046\u753b\u50cf\u5909\u63db\u30bd\u30d5\u30c8\u304c\u4f7f\u3048\u3001gif\u30a2\u30cb\u30e1\u304c\u4f5c\u308c\u307e\u3059\u3002\n",
"\u3053\u306e\u30bd\u30d5\u30c8\u306fpython\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u306f\u7121\u304f\u3066\u3001\u5916\u90e8\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306a\u306e\u3067\u3001\u5225\u9014\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001\n",
"\u4fbf\u5229\u3067\u30e1\u30b8\u30e3\u30fc\u306a\u30bd\u30d5\u30c8\u306a\u306e\u3067\u3001\u5165\u308c\u3066\u304a\u3044\u3066\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\n",
"\n",
"Gif\u30a2\u30cb\u30e1\u306f\u30b3\u30de\u9001\u308a\u306e\u30a2\u30cb\u30e1\u306a\u306e\u3067\u3001\u7c97\u3044\u6642\u9593\u9593\u9694\u3067\u8a08\u7b97\u3055\u308c\u305f\u7269\u7406\u72b6\u614b\u5909\u5316\u3092\u898b\u305b\u305f\u3044\u5834\u5408\u306b\u5411\u3044\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002\n",
"\u307e\u305f\u3001markdown\u4e0a\u306b\u3082html\u306b\u3082\u753b\u50cf\u3068\u3057\u3066\u8cbc\u308c\u308b\u306e\u3067\u3001\u6271\u3044\u304c\u5bb9\u6613\u3067\u3059\u3002\n",
"\n",
"\u305f\u3060\u3057\u3001Windows\u3067\u306f\u6ce8\u610f\u70b9\u304c\u3042\u308a\u307e\u3059\u3002\n",
"Imagemagick\u306econvert.exe\u3068\u3044\u3046\u306e\u3092\u5185\u90e8\u3067\u5229\u7528\u3059\u308b\u306e\u3067\u3059\u304c\u3001windows\u306e\u30b7\u30b9\u30c6\u30e0\u5185\u306b\u3042\u308b\u5225\u306econvert\u3068\u3044\u3046\u30b3\u30de\u30f3\u30c9\u3068\u9593\u9055\u3048\u308b\u6050\u308c\u304c\u3042\u308a\u307e\u3059\u3002\n",
"\n",
"[\u53c2\u8003\u30b5\u30a4\u30c8\uff1ahttp://stackoverflow.com/questions/3060205/error-invalid-parameter-fom-imagemagick-convert-on-windows](http://stackoverflow.com/questions/3060205/error-invalid-parameter-fom-imagemagick-convert-on-windows)\n",
"\n",
"\n",
"\u305d\u3053\u3067\u3001Imagemagick\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092matplotlibrc\uff08matplotlib\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\uff09\u306b\u767b\u9332\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\n",
"\n",
"\u30e6\u30fc\u30b6\u30fc\u306e<pre>.matplotlib\\matplotlibrc</pre>\u3092\u30c6\u30ad\u30b9\u30c8\u30a8\u30c7\u30a3\u30bf\u3067\u958b\u3044\u3066\u3001\u6700\u5f8c\u306e\u884c\u306b\n",
"\n",
"<pre>animation.convert_path: C:\\Program Files\\ImageMagick-6.8.6-Q16\\convert</pre>\n",
"\n",
"\u306a\u3069\u3068\u9069\u5207\u306a\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u540d\u3068\u30b3\u30de\u30f3\u30c9\u540d\u3092\u5165\u308c\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib qt"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\u901a\u5e38\u3001\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u306f\u5916\u90e8Window\u4e0a\u3067\u898b\u307e\u3059\u306e\u3067\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from matplotlib import animation\n",
"\n",
"# Set up the axes, making sure the axis ratio is equal\n",
"plt.figure(figsize=(5,3))\n",
"line,=plt.plot(0,0,'ko')\n",
"title=plt.title('')\n",
"plt.xlabel('x(m)')\n",
"plt.ylabel('y(m)')\n",
"plt.xlim(0,30)\n",
"plt.ylim(0,12)\n",
"plt.grid()\n",
"fig=plt.gcf()\n",
"ax=plt.gca()\n",
"ax.set_aspect('equal')\n",
"\n",
"# This function moves the polygons as a function of the frame i\n",
"Nframes = 600\n",
"def animate(nframe):\n",
" time=(nframe%60)*50.0e-3\n",
" v=16.0\n",
" g=9.8\n",
" theta=20.0+(nframe/60)*5\n",
" vx=v*np.cos(theta*np.pi/180.0)\n",
" vy=v*np.sin(theta*np.pi/180.0)\n",
" line.set_data(vx*time,vy*time-0.5*g*time**2)\n",
" title.set_text(u'\u6295\u3052\u4e0a\u3052\u89d2\u5ea6\uff1a'+str(theta)+u'\u5ea6')\n",
" return line,title\n",
" \n",
"anim = animation.FuncAnimation(plt.gcf(), animate,\n",
" frames=Nframes, interval=50)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"qt\u30a6\u30a3\u30f3\u30c9\u30a6\u4e0a\u3067\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u304c\u898b\u3048\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002\n",
"\u3053\u308c\u3092\u6b8b\u3057\u305f\u307e\u307e\u3067\u3082\u3001\u9589\u3058\u3066\u3057\u307e\u3063\u3066\u3082\u3088\u3044\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308c\u3070\u3001gif\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u304c\u51fa\u6765\u4e0a\u304c\u308a\u307e\u3059\u3002"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"anim.save('throw_up.gif',fps=20, writer='imagemagick')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\u3053\u308c\u3092\n",
"\n",
"<pre>![image](files/throw_up.gif)</pre> \n",
"\n",
"\u306a\u3069\u3068\u3057\u3066\u3001markdown\u30bb\u30eb\u306b\u8cbc\u308a\u4ed8\u3051\u308c\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\n",
"\n",
"\n",
"![image](files/throw_up.gif)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\u3053\u308c\u3092\u4e00\u822c\u7684\u306a\u52d5\u753b\u5f62\u5f0f\u3068\u6bd4\u3079\u3066\u307f\u307e\u3057\u3087\u3046\u3002"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"FFMpegWriter = animation.writers['ffmpeg']\n",
"metadata = dict(title='Movie Test', artist='Matplotlib',\n",
" comment='Movie support!')\n",
"writer = FFMpegWriter(fps=15, metadata=metadata)\n",
"with writer.saving(fig, \"writer_test.mp4\", 100):\n",
" for i in range(Nframes):\n",
" animate(i)\n",
" writer.grab_frame()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\u4ee5\u4e0a\u306e\u3088\u3046\u306b\u3059\u308b\u3068\u3001mp4\u3068\u3044\u3046\u5f62\u5f0f\u306e\u52d5\u753b\u30d5\u30a1\u30a4\u30eb\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u30ce\u30fc\u30c8\u30d6\u30c3\u30af\u4e0a\u306b\u8cbc\u308a\u4ed8\u3051\u308b\u4e8b\u306f\u3067\u304d\u307e\u305b\u3093\u3002 \u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u3092\u898b\u308b\u3068\u3001\u4f55\u3068\u304b\u3067\u304d\u308b\u306e\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u304c\u3001\u79c1\u306b\u306f\u307e\u3060\u7406\u89e3\u3067\u304d\u3066\u3044\u307e\u305b\u3093\u3002\n",
"\n",
"[\u53c2\u8003\u30b5\u30a4\u30c8\uff1a\u3000http://jakevdp.github.io/blog/2013/05/12/embedding-matplotlib-animations/](http://jakevdp.github.io/blog/2013/05/12/embedding-matplotlib-animations/)\n",
"\n",
"youtube\u306b\u6295\u7a3f\u3059\u308c\u3070\u3001\u8cbc\u308a\u3053\u3081\u308b\u3088\u3046\u3067\u3059\u304c\u3001\u30ed\u30fc\u30ab\u30eb\u306b\u7f6e\u3051\u306a\u3044\u306e\u306f\u4e0d\u4fbf\u3067\u3059\u306d\u3002\n",
"Blogger\u3067\u306f\u52d5\u753b\u3068\u3057\u3066\u8cbc\u308a\u8fbc\u3081\u307e\u3059\u306e\u3067\u3001\u6bd4\u8f03\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 mp4\u306e\u65b9\u304c\u30b5\u30a4\u30ba\u306f\u5c0f\u3055\u3044\u3088\u3046\u3067\u3059\u306e\u3067\u3001\u591a\u6570\u3084\u9577\u6642\u9593\u306e\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3092\u516c\u958b\u3059\u308b\u5834\u5408\u306b\u306f\u3053\u3061\u3089\u306e\u65b9\u304c\u826f\u3044\u306e\u304b\u3082\u77e5\u308c\u307e\u305b\u3093"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment