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

.. only:: html

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

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

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

.. _sphx_glr_gallery_subplots_axes_and_figures_demo_constrained_layout.py:


=====================================
Resizing axes with constrained layout
=====================================

Constrained layout attempts to resize subplots in
a figure so that there are no overlaps between axes objects and labels
on the axes.

See :doc:`/tutorials/intermediate/constrainedlayout_guide` for more details and
:doc:`/tutorials/intermediate/tight_layout_guide` for an alternative.

.. GENERATED FROM PYTHON SOURCE LINES 14-25

.. code-block:: default


    import matplotlib.pyplot as plt


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









.. GENERATED FROM PYTHON SOURCE LINES 26-27

If we don't use constrained_layout, then labels overlap the axes

.. GENERATED FROM PYTHON SOURCE LINES 27-33

.. code-block:: default


    fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=False)

    for ax in axs.flat:
        example_plot(ax)




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_constrained_layout_001.png
    :alt: Title, Title, Title, Title
    :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 34-35

adding ``constrained_layout=True`` automatically adjusts.

.. GENERATED FROM PYTHON SOURCE LINES 35-41

.. code-block:: default


    fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True)

    for ax in axs.flat:
        example_plot(ax)




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_constrained_layout_002.png
    :alt: Title, Title, Title, Title
    :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 42-43

Below is a more complicated example using nested gridspecs.

.. GENERATED FROM PYTHON SOURCE LINES 43-63

.. code-block:: default


    fig = plt.figure(constrained_layout=True)

    import matplotlib.gridspec as gridspec

    gs0 = gridspec.GridSpec(1, 2, figure=fig)

    gs1 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=gs0[0])
    for n in range(3):
        ax = fig.add_subplot(gs1[n])
        example_plot(ax)


    gs2 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[1])
    for n in range(2):
        ax = fig.add_subplot(gs2[n])
        example_plot(ax)

    plt.show()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_constrained_layout_003.png
    :alt: Title, Title, Title, Title, Title
    :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 64-70

------------

References
""""""""""

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

.. GENERATED FROM PYTHON SOURCE LINES 71-75

.. code-block:: default


    import matplotlib
    matplotlib.gridspec.GridSpec
    matplotlib.gridspec.GridSpecFromSubplotSpec








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

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


.. _sphx_glr_download_gallery_subplots_axes_and_figures_demo_constrained_layout.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: demo_constrained_layout.py <demo_constrained_layout.py>`



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

     :download:`Download Jupyter notebook: demo_constrained_layout.ipynb <demo_constrained_layout.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>`_
