.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\matplotlib\demo_matplotlibAnimation2d.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_matplotlib_demo_matplotlibAnimation2d.py: 2D animation =============== This example demonstrates how matplotlib can be used to create animated movie and export these in the mp4 movie format. .. GENERATED FROM PYTHON SOURCE LINES 8-37 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation fig, ax = plt.subplots() def f(x, y): return np.sin(x) + np.cos(y) x = np.linspace(0, 2 * np.pi, 120) y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) # ims is a list of lists, each row is a list of artists to draw in the # current frame; here we are just animating one artist, the image, in # each frame ims = [] for i in range(60): x += np.pi / 15.0 y += np.pi / 20.0 im = ax.imshow(f(x, y), animated=True) if i == 0: ax.imshow(f(x, y)) # show an initial one first ims.append([im]) ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, repeat_delay=1000) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 38-51 To save the animation, use e.g. .. code-block:: python ani.save("movie.mp4") or .. code-block:: python writer = animation.FFMpegWriter( fps=15, metadata=dict(artist='Me'), bitrate=1800) ani.save("movie.mp4", writer=writer) .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.436 seconds) .. _sphx_glr_download_11_demos_python_packages_matplotlib_demo_matplotlibAnimation2d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_matplotlibAnimation2d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_matplotlibAnimation2d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_matplotlibAnimation2d.zip `