.. 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_tight_layout.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_subplots_axes_and_figures_demo_tight_layout.py: =============================== Resizing axes with tight layout =============================== `~.Figure.tight_layout` attempts to resize subplots in a figure so that there are no overlaps between axes objects and labels on the axes. See :ref:`tight_layout_guide` for more details and :ref:`constrainedlayout_guide` for an alternative. .. GENERATED FROM PYTHON SOURCE LINES 13-29 .. code-block:: Python import itertools import warnings import matplotlib.pyplot as plt 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)) .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: Python fig, ax = plt.subplots() example_plot(ax) fig.tight_layout() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_001.png :alt: Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_001.png, /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-44 .. 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) fig.tight_layout() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_002.png :alt: Title, Title, Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_002.png, /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_002_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-51 .. code-block:: Python fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1) example_plot(ax1) example_plot(ax2) fig.tight_layout() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_003.png :alt: Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_003.png, /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_003_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 52-58 .. code-block:: Python fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2) example_plot(ax1) example_plot(ax2) fig.tight_layout() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_004.png :alt: Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_004.png, /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_004_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-65 .. code-block:: Python fig, axs = plt.subplots(nrows=3, ncols=3) for ax in axs.flat: example_plot(ax) fig.tight_layout() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_005.png :alt: Title, Title, Title, Title, Title, Title, Title, Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_005.png, /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_005_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-76 .. code-block:: Python 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-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_006.png :alt: Title, Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_006.png, /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_006_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-89 .. code-block:: Python 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() .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_007.png :alt: Title, Title, Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_007.png, /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_007_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-123 .. code-block:: Python fig = plt.figure() gs1 = fig.add_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) gs1.tight_layout(fig, rect=[None, None, 0.45, None]) gs2 = fig.add_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(): # gs2.tight_layout cannot handle the subplots from the first gridspec # (gs1), so it will raise a warning. We are going to match the gridspecs # manually so we can filter the warning away. 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-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_008.png :alt: Title, Title, Title, Title, Title :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_008.png, /gallery/subplots_axes_and_figures/images/sphx_glr_demo_tight_layout_008_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 124-134 .. admonition:: References The use of the following functions, methods, classes and modules is shown in this example: - `matplotlib.figure.Figure.tight_layout` / `matplotlib.pyplot.tight_layout` - `matplotlib.figure.Figure.add_gridspec` - `matplotlib.figure.Figure.add_subplot` - `matplotlib.pyplot.subplot2grid` .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.721 seconds) .. _sphx_glr_download_gallery_subplots_axes_and_figures_demo_tight_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_tight_layout.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_tight_layout.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_