

.. _sphx_glr_gallery_subplots_axes_and_figures_demo_tight_layout.py:


=================
Demo Tight Layout
=================




.. code-block:: python


    import matplotlib.pyplot as plt
    import itertools
    import warnings


    fontsizes = itertools.cycle([8, 16, 24, 32])


    def example_plot(ax):
        ax.plot([1, 2])
        ax.set_xlabel('x-label', fontsize=next(fontsizes))
        ax.set_ylabel('y-label', fontsize=next(fontsizes))
        ax.set_title('Title', fontsize=next(fontsizes))









.. code-block:: python


    fig, ax = plt.subplots()
    example_plot(ax)
    plt.tight_layout()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_001.png
    :align: center





.. code-block:: python


    fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
    example_plot(ax1)
    example_plot(ax2)
    example_plot(ax3)
    example_plot(ax4)
    plt.tight_layout()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_002.png
    :align: center





.. code-block:: python


    fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
    example_plot(ax1)
    example_plot(ax2)
    plt.tight_layout()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_003.png
    :align: center





.. code-block:: python


    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
    example_plot(ax1)
    example_plot(ax2)
    plt.tight_layout()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_004.png
    :align: center





.. code-block:: python


    fig, axes = plt.subplots(nrows=3, ncols=3)
    for row in axes:
        for ax in row:
            example_plot(ax)
    plt.tight_layout()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_005.png
    :align: center





.. code-block:: python


    fig = plt.figure()

    ax1 = plt.subplot(221)
    ax2 = plt.subplot(223)
    ax3 = plt.subplot(122)

    example_plot(ax1)
    example_plot(ax2)
    example_plot(ax3)

    plt.tight_layout()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_006.png
    :align: center





.. code-block:: python


    fig = plt.figure()

    ax1 = plt.subplot2grid((3, 3), (0, 0))
    ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
    ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
    ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)

    example_plot(ax1)
    example_plot(ax2)
    example_plot(ax3)
    example_plot(ax4)

    plt.tight_layout()

    plt.show()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_007.png
    :align: center





.. code-block:: python


    fig = plt.figure()

    import matplotlib.gridspec as gridspec

    gs1 = gridspec.GridSpec(3, 1)
    ax1 = fig.add_subplot(gs1[0])
    ax2 = fig.add_subplot(gs1[1])
    ax3 = fig.add_subplot(gs1[2])

    example_plot(ax1)
    example_plot(ax2)
    example_plot(ax3)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", UserWarning)
        # This raises warnings since tight layout cannot
        # handle gridspec automatically. We are going to
        # do that manually so we can filter the warning.
        gs1.tight_layout(fig, rect=[None, None, 0.45, None])

    gs2 = gridspec.GridSpec(2, 1)
    ax4 = fig.add_subplot(gs2[0])
    ax5 = fig.add_subplot(gs2[1])

    example_plot(ax4)
    example_plot(ax5)

    with warnings.catch_warnings():
        # This raises warnings since tight layout cannot
        # handle gridspec automatically. We are going to
        # do that manually so we can filter the warning.
        warnings.simplefilter("ignore", UserWarning)
        gs2.tight_layout(fig, rect=[0.45, None, None, None])

    # now match the top and bottom of two gridspecs.
    top = min(gs1.top, gs2.top)
    bottom = max(gs1.bottom, gs2.bottom)

    gs1.update(top=top, bottom=bottom)
    gs2.update(top=top, bottom=bottom)

    plt.show()



.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_008.png
    :align: center




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



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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