You are reading an old version of the documentation (v3.1.1). For the latest version see https://matplotlib.org/stable/gallery/subplots_axes_and_figures/demo_tight_layout.html
Version 3.1.2
matplotlib
Fork me on GitHub

Table of Contents

Related Topics

Resizing axes with tight layout

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

See Tight Layout guide for more details and Constrained Layout Guide for an alternative.

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))
../../_images/sphx_glr_demo_tight_layout_001.png ../../_images/sphx_glr_demo_tight_layout_002.png ../../_images/sphx_glr_demo_tight_layout_003.png ../../_images/sphx_glr_demo_tight_layout_004.png
fig, axes = plt.subplots(nrows=3, ncols=3)
for row in axes:
    for ax in row:
        example_plot(ax)
plt.tight_layout()
../../_images/sphx_glr_demo_tight_layout_005.png ../../_images/sphx_glr_demo_tight_layout_006.png
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()
../../_images/sphx_glr_demo_tight_layout_007.png
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()
../../_images/sphx_glr_demo_tight_layout_008.png

References

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

import matplotlib
matplotlib.pyplot.tight_layout
matplotlib.figure.Figure.tight_layout
matplotlib.figure.Figure.add_gridspec
matplotlib.figure.Figure.add_subplot
matplotlib.pyplot.subplot2grid

Out:

<function subplot2grid at 0x7fb11b42c488>

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

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery