Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use tight_layout() to adjust subplot spacing and margins; use bbox_inches="tight" in savefig() to trim excess whitespace around the saved figure. They solve different problems, so you can use both when needed.

Use both settings in a basic save workflow

Create and label the plot, adjust its layout, then save it with a tight output boundary:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [2, 1, 4])
ax.set_xlabel("X value")
ax.set_ylabel("Y value")
ax.set_title("Example")

fig.tight_layout()  # Adjust subplot spacing and margins
fig.savefig("plot.png", bbox_inches="tight", pad_inches=0.1)

fig.tight_layout() adjusts the figure’s subplot parameters at the point where you call it. The equivalent pyplot call, plt.tight_layout(), adjusts the current figure. For automatic adjustment on redraw, Matplotlib documents fig.set_tight_layout(True) and rcParams["figure.autolayout"] = True.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What each “tight” option changes

Option What it changes When to use it
tight_layout() Subplot geometry: spacing and margins within the figure. When axes decorations or neighboring subplots need more room.
bbox_inches="tight" The bounds of the saved output, so the exported image or vector graphic contains the figure’s tight bounding box. When the saved file has unwanted whitespace around the figure.
pad_inches Padding around the tight saved bounding box. Matplotlib’s documented default is 0.1 inches. When you want to control the whitespace around the cropped output.

Because layout adjustment and export cropping happen at different stages, combining fig.tight_layout() with fig.savefig(..., bbox_inches="tight") is valid. The Matplotlib Tight layout guide and savefig API documentation describe these separate roles.

For complex figures, consider constrained layout

Matplotlib’s current guide describes constrained layout as more flexible than tight layout, particularly with colorbars, nested layouts, axes spanning rows or columns, and alignment. Enable it when creating the figure:

fig, ax = plt.subplots(layout="constrained")

Choose a layout engine deliberately. Calling tight_layout() disables constrained layout, so do not call it if you intend to keep constrained layout active. See the Constrained layout guide for its behavior and examples.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot clipped labels, legends, or titles

  • Check the artist’s layout inclusion. An artist’s set_in_layout(bool) setting controls whether it participates in layout and tight-bounding-box calculations. An artist excluded from those calculations may be cropped. The Artist.set_in_layout reference documents this control.
  • Keep positive padding. The tight-layout guide warns that pad=0 can clip text by a few pixels and recommends padding greater than 0.3 for that layout setting. For savefig(), pad_inches controls padding around the saved tight bounding box; its documented default is 0.1 inches.
  • Do not expect repeated calls to converge exactly. Matplotlib notes that repeated tight-layout calls may vary slightly because the algorithm does not necessarily converge. The algorithm considers extents such as tick labels, axis labels, and titles, but assumes that the extra space needed is independent of an Axes’ original position; that assumption can fail in rare cases.
  • For a legend outside the axes, follow the constrained-layout workflow if using that engine. Its guide documents a more involved process that toggles an artist’s layout inclusion and triggers a draw before saving; simply excluding an artist can leave it out of the calculated bounds.

These details are covered in the Matplotlib Tight layout guide and Constrained layout guide.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.