

.. _sphx_glr_gallery_animation_dynamic_image2.py:


========================================
An animated image using a list of images
========================================

Animate an image from a list of images (or Artists).




.. image:: /gallery/animation/images/sphx_glr_dynamic_image2_001.png
    :align: center





.. code-block:: python

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation

    fig = plt.figure()


    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.
        y += np.pi / 20.
        im = plt.imshow(f(x, y), animated=True)
        ims.append([im])

    ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                    repeat_delay=1000)

    # ani.save('dynamic_images.mp4')

    plt.show()

**Total running time of the script:** ( 0 minutes  1.113 seconds)



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

     :download:`Download Python source code: dynamic_image2.py <dynamic_image2.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: dynamic_image2.ipynb <dynamic_image2.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
