Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

How to Use tight_layout and bbox_inches in Matplotlib

Matplotlib’s tight_layout adjusts subplot spacing; bbox_inches="tight" trims the saved figure bounds. See how to use both and troubleshoot clipping.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use tight_layout() to adjust spacing between Matplotlib subplots and their labels; use bbox_inches="tight" when saving to trim excess space around the figure. They solve different problems and can be used together. For complex layouts, Matplotlib’s current guide generally favors constrained layout instead.

What each option does

Option What it changes When it helps
fig.tight_layout() Adjusts subplot parameters, including spacing and margins, so Axes decorations fit more cleanly within the figure. It is a layout adjustment, not an export crop. Matplotlib Tight layout guide Use it when neighboring subplots, axis labels, tick labels, or titles are crowded or overlap.
bbox_inches="tight" In savefig(), calculates a tight bounding box and saves that portion of the figure. It changes the saved output bounds, not subplot geometry. Matplotlib savefig API Use it when the exported image or vector graphic has too much whitespace around the plotted content.
pad_inches Adds padding around the tight saved bounding box; the documented default is 0.1 inches. Matplotlib savefig API Adjust it when you want more or less breathing room around the saved figure.

Use both in a basic save workflow

First create and label the plot, then adjust its subplot layout and save with a tight output boundary:

As an Amazon Associate I earn from qualifying purchases.

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() operates on that figure. The equivalent plt.tight_layout() adjusts the current figure. The call applies the adjustment when it runs; for automatic adjustment on redraw, Matplotlib documents fig.set_tight_layout(True) and rcParams["figure.autolayout"] = True. Matplotlib Tight layout guide

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

Choose a layout method for complex figures

For colorbars, nested layouts, Axes spanning rows or columns, and alignment across a more complicated grid, Matplotlib’s current guide describes constrained layout as more flexible than tight_layout(). Enable it when creating the figure:

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

Choose one layout engine deliberately. Calling tight_layout() disables constrained layout, so do not call it if you want constrained layout to remain active. Matplotlib Constrained layout guide

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

Troubleshoot clipped labels and legends

Check whether an artist is included

If a label, legend, or other decoration is still clipped, check whether that artist participates in layout and tight-bounding-box calculations. Artist.set_in_layout(bool) controls its inclusion; excluding an artist can cause it to be cropped. The constrained-layout guide describes a more involved legend workflow that toggles inclusion and triggers a draw before saving. Matplotlib Artist.set_in_layout reference Matplotlib Constrained layout guide

Keep some padding

For a tight saved bounding box, pad_inches controls the extra margin. Although the documented default is 0.1 inches, 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 adjustment. Matplotlib Tight layout guide Matplotlib savefig API

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

Do not rely on repeated calls to converge

tight_layout() estimates space from artist extents such as tick labels, axis labels, and titles. Its assumption that the extra space needed is independent of an Axes’ original position can fail in rare cases, and repeated calls may produce small variations because the algorithm does not necessarily converge. Matplotlib Tight layout guide

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.