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

.. only:: html

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

        Click :ref:`here <sphx_glr_download_gallery_recipes_create_subplots.py>`
        to download the full example code

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

.. _sphx_glr_gallery_recipes_create_subplots.py:


Easily creating subplots
========================

In early versions of matplotlib, if you wanted to use the pythonic API
and create a figure instance and from that create a grid of subplots,
possibly with shared axes, it involved a fair amount of boilerplate
code.  e.g.

.. GENERATED FROM PYTHON SOURCE LINES 10-27

.. code-block:: default


    import matplotlib.pyplot as plt
    import numpy as np


    # Fixing random state for reproducibility
    np.random.seed(19680801)

    x = np.random.randn(50)

    # old style
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222, sharex=ax1, sharey=ax1)
    ax3 = fig.add_subplot(223, sharex=ax1, sharey=ax1)
    ax3 = fig.add_subplot(224, sharex=ax1, sharey=ax1)




.. image:: /gallery/recipes/images/sphx_glr_create_subplots_001.png
    :alt: create subplots
    :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 28-32

Fernando Perez has provided the nice top-level function
`~matplotlib.pyplot.subplots` (note the "s" at the end) to create
everything at once, and turn on x and y sharing for the whole bunch.
You can either unpack the axes individually...

.. GENERATED FROM PYTHON SOURCE LINES 32-37

.. code-block:: default


    # new style method 1; unpack the axes
    fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)
    ax1.plot(x)




.. image:: /gallery/recipes/images/sphx_glr_create_subplots_002.png
    :alt: create subplots
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none


    [<matplotlib.lines.Line2D object at 0x7f73b4c1ea60>]



.. GENERATED FROM PYTHON SOURCE LINES 38-40

or get them back as a numrows x numcolumns object array which supports
numpy indexing

.. GENERATED FROM PYTHON SOURCE LINES 40-46

.. code-block:: default


    # new style method 2; use an axes array
    fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
    axs[0, 0].plot(x)

    plt.show()



.. image:: /gallery/recipes/images/sphx_glr_create_subplots_003.png
    :alt: create subplots
    :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_gallery_recipes_create_subplots.py:


.. only :: html

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



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

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



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

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
