Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

Matplotlib tight_layout(): Fix Overlapping Subplots and Labels

Call fig.tight_layout() after labeling a conventional subplot grid. For colorbars, legends, nested subfigures, or spanning axes, enable constrained layout when creating the figure.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a conventional Matplotlib subplot grid, call fig.tight_layout() after adding titles and axis labels. It adjusts subplot spacing when called so common decorations fit within the figure. For more complex layouts involving colorbars, legends, nested subfigures, or axes that span rows or columns, enable constrained layout when creating the figure instead.

Fix overlapping labels with tight_layout()

Call fig.tight_layout() after creating the axes and setting their titles and labels, but before displaying or saving the figure:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)
for ax in axs.flat:
    ax.set_xlabel("X label")
    ax.set_ylabel("Y label")
    ax.set_title("Panel title")

fig.tight_layout()
plt.show()

The function adjusts subplot parameters at the time you call it. Matplotlib’s tight-layout guide describes its scope as tick labels, axis labels, and titles. If you change labels or titles after calling it, call it again to recalculate the spacing.

When to use constrained layout instead

For a new figure with more involved decorations or grid geometry, try constrained layout from figure creation:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
fig, axs = plt.subplots(2, 2, layout="constrained")

The constrained-layout guide documents automatic adjustment for decorations including tick labels, legends, and colorbars. It is more flexible for colorbars shared across multiple axes, nested subfigures, and axes spanning rows or columns. Matplotlib’s layout-engine API describes constrained layout as the more modern built-in engine and generally better performing than tight layout.

Enable constrained layout before adding axes. Do not call tight_layout() afterward: doing so turns constrained layout off.

Choose a layout method for your figure

Method When it adjusts Documented scope and fit
fig.tight_layout() When called Tick labels, axis labels, and titles; a practical one-time adjustment for conventional subplot grids. Matplotlib’s tight-layout guide documents this scope.
Constrained layout Enabled when the figure is created and adjusts layout automatically Broader decorations such as legends and colorbars, including more complex grids. See Matplotlib’s constrained-layout guide.
fig.set_tight_layout(True) or rcParams["figure.autolayout"] = True On each redraw Options documented by the tight-layout guide when repeated tight-layout adjustment is wanted.
fig.subplots_adjust(...) When called Manual subplot positioning for precise margin control; documented as an alternative in the tight-layout guide.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

If labels still overlap

Automatic layout is not a guarantee against every collision, particularly with custom artists or unusually long text. After rendering, try the adjustment best suited to the cause:

  • Increase the figure size if the panels have too little room.
  • Shorten or rotate long tick labels, or revise lengthy axis labels and titles.
  • Use fig.subplots_adjust(...) when a specific margin needs manual control.
  • For colorbars, legends, nested subfigures, or spanning axes, use constrained layout from figure creation and inspect the result.

The tight-layout guide is version 3.6.2 documentation; the cited constrained-layout guide and layout-engine API are from Matplotlib 3.11.2 stable documentation. The methods’ documented capabilities help guide the choice, but the rendered result depends on the figure and its artists.

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.

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
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver 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.