

.. _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.



.. code-block:: python


    import matplotlib.pyplot as plt
    import numpy as np

    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
    :align: center




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



.. code-block:: python


    # 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
    :align: center




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



.. code-block:: python


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



.. image:: /gallery/recipes/images/sphx_glr_create_subplots_003.png
    :align: center




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



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

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