{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1D animation\n\nThis example demonstrates how matplotlib can be used to create \nanimated movie and export these in the mp4 movie format. \nIt is shown here with some random generated 2d images, which ware plotted via matplotlib. \nBy using the figure handle the animation is created. So you can plot your matplot\nfigures in your own way and used some similar syntax to create an animation. \n\nFirst of all you must install the matplotlib package, e.g. from\nhttps://pypi.org/project/matplotlib/\n\nThen you must install the ffmpeg codec. A detailed description can be found on: \nhttp://blog.gregzaal.com/how-to-install-ffmpeg-on-windows/\n\nThe build version of the ffmpeg codec can be downloaded here: \nhttp://ffmpeg.zeranoe.com/builds/\n\nDownload and unzip the builds files to your harddrive. Typically the folder is like: \nC:\\Program files\\ffmpeg\n\nThe bin folder of ffmpeg must be added to the path variables of your system: \nC:\\Program files\\ffmpeg\\bin \n\nFinally start the command prompt and run the command: \nC:\\Program files\\ffmpeg\\bin\\ffmpeg.exe -codecs\n\nor easier: \nffmpeg -codecs\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\n\nfig, ax = plt.subplots()\n\nx = np.arange(0, 2*np.pi, 0.01)\nline, = ax.plot(x, np.sin(x))\n\n\ndef animate(i):\n line.set_ydata(np.sin(x + i / 50)) # update the data.\n return line,\n\n\nani = animation.FuncAnimation(\n fig, animate, interval=20, blit=True, save_count=50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To save the animation, use e.g.\n\n```python\nani.save(\"movie.mp4\")\n```\nor\n\n```python\nwriter = animation.FFMpegWriter(\nfps=15, metadata=dict(artist='Me'), bitrate=1800)\nani.save(\"movie.mp4\", writer=writer)\n```\nPlease consider that this requires the ffmpeg installed on your computer.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }