
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/animation/multiple_axes.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_animation_multiple_axes.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_animation_multiple_axes.py:


=======================
Multiple Axes animation
=======================

This example showcases:

- how animation across multiple subplots works,
- using a figure artist in the animation.

Output generated via `matplotlib.animation.Animation.to_jshtml`.

.. GENERATED FROM PYTHON SOURCE LINES 13-74

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    import matplotlib.animation as animation
    from matplotlib.patches import ConnectionPatch

    fig, (axl, axr) = plt.subplots(
        ncols=2,
        sharey=True,
        figsize=(6, 2),
        gridspec_kw=dict(width_ratios=[1, 3], wspace=0),
    )
    axl.set_aspect(1)
    axr.set_box_aspect(1 / 3)
    axr.yaxis.set_visible(False)
    axr.xaxis.set_ticks([0, np.pi, 2 * np.pi], ["0", r"$\pi$", r"$2\pi$"])

    # draw circle with initial point in left Axes
    x = np.linspace(0, 2 * np.pi, 50)
    axl.plot(np.cos(x), np.sin(x), "k", lw=0.3)
    point, = axl.plot(0, 0, "o")

    # draw full curve to set view limits in right Axes
    sine, = axr.plot(x, np.sin(x))

    # draw connecting line between both graphs
    con = ConnectionPatch(
        (1, 0),
        (0, 0),
        "data",
        "data",
        axesA=axl,
        axesB=axr,
        color="C0",
        ls="dotted",
    )
    fig.add_artist(con)


    def animate(i):
        x = np.linspace(0, i, int(i * 25 / np.pi))
        sine.set_data(x, np.sin(x))
        x, y = np.cos(i), np.sin(i)
        point.set_data([x], [y])
        con.xy1 = x, y
        con.xy2 = i, y
        return point, sine, con


    ani = animation.FuncAnimation(
        fig,
        animate,
        interval=50,
        blit=False,  # blitting can't be used with Figure artists
        frames=x,
        repeat_delay=100,
    )

    plt.show()


.. GENERATED FROM PYTHON SOURCE LINES 75-84

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.patches.ConnectionPatch`
   - `matplotlib.animation.FuncAnimation`

.. tags:: component: axes, animation


.. _sphx_glr_download_gallery_animation_multiple_axes.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

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

    .. container:: sphx-glr-download sphx-glr-download-python

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: multiple_axes.zip <multiple_axes.zip>`


.. only:: html

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

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