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

    .. meta::
        :keywords: codex

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

        :ref:`Go to the end <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:


===================================
Resize 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 :ref:`constrainedlayout_guide` for more details and
:ref:`tight_layout_guide` for an alternative.

.. GENERATED FROM PYTHON SOURCE LINES 14-25

.. code-block:: Python


    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:: Python


    fig, axs = plt.subplots(nrows=2, ncols=2, layout=None)

    for ax in axs.flat:
        example_plot(ax)


.. GENERATED FROM PYTHON SOURCE LINES 34-35

adding ``layout='constrained'`` automatically adjusts.

.. GENERATED FROM PYTHON SOURCE LINES 35-41

.. code-block:: Python


    fig, axs = plt.subplots(nrows=2, ncols=2, layout='constrained')

    for ax in axs.flat:
        example_plot(ax)


.. 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:: Python


    fig = plt.figure(layout='constrained')

    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()


.. GENERATED FROM PYTHON SOURCE LINES 64-78

.. admonition:: References

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

   - `matplotlib.gridspec.GridSpec`
   - `matplotlib.gridspec.GridSpecFromSubplotSpec`

.. tags::

   component: axes
   component: subplot
   styling: size
   level: beginner


.. _sphx_glr_download_gallery_subplots_axes_and_figures_demo_constrained_layout.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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