
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/subplots_axes_and_figures/multiple_figs_demo.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_subplots_axes_and_figures_multiple_figs_demo.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_subplots_axes_and_figures_multiple_figs_demo.py:


=================================
Manage multiple figures in pyplot
=================================

`matplotlib.pyplot` uses the concept of a *current figure* and *current Axes*.
Figures are identified via a figure number that is passed to `~.pyplot.figure`.
The figure with the given number is set as *current figure*. Additionally, if
no figure with the number exists, a new one is created.

.. note::

    We discourage working with multiple figures through the implicit pyplot
    interface because managing the *current figure* is cumbersome and
    error-prone. Instead, we recommend using the explicit approach and call
    methods on Figure and Axes instances. See :ref:`api_interfaces` for an
    explanation of the trade-offs between the implicit and explicit interfaces.

.. GENERATED FROM PYTHON SOURCE LINES 20-27

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    t = np.arange(0.0, 2.0, 0.01)
    s1 = np.sin(2*np.pi*t)
    s2 = np.sin(4*np.pi*t)


.. GENERATED FROM PYTHON SOURCE LINES 28-29

Create figure 1

.. GENERATED FROM PYTHON SOURCE LINES 29-36

.. code-block:: Python


    plt.figure(1)
    plt.subplot(211)
    plt.plot(t, s1)
    plt.subplot(212)
    plt.plot(t, 2*s1)


.. GENERATED FROM PYTHON SOURCE LINES 37-38

Create figure 2

.. GENERATED FROM PYTHON SOURCE LINES 38-42

.. code-block:: Python


    plt.figure(2)
    plt.plot(t, s2)


.. GENERATED FROM PYTHON SOURCE LINES 43-44

Now switch back to figure 1 and make some changes

.. GENERATED FROM PYTHON SOURCE LINES 44-53

.. code-block:: Python


    plt.figure(1)
    plt.subplot(211)
    plt.plot(t, s2, 's')
    ax = plt.gca()
    ax.set_xticklabels([])

    plt.show()


.. GENERATED FROM PYTHON SOURCE LINES 54-55

.. tags:: component: figure, plot-type: line


.. _sphx_glr_download_gallery_subplots_axes_and_figures_multiple_figs_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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