DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober 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

Matplotlib Constrained Layout vs. Tight Layout: Which Should You Use?

Use Matplotlib's constrained layout for flexible spacing in new or complex figures; use tight_layout() for a straightforward subplot-spacing adjustment.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For most new Matplotlib figures, use layout="constrained": it is more flexible about making room for supported labels, legends, colorbars, and complex subplot arrangements. Use tight_layout() when a simple existing figure needs a one-time spacing adjustment with direct padding controls. Do not call tight_layout() after enabling constrained layout; it turns that layout engine off.

How the two layout options differ

Aspect Constrained layout Tight layout
How it works Runs as the figure is drawn and adjusts axes to make room for supported decorations. Directly adjusts spacing around subplots.
Best fit Figures with colorbars, nested subfigures, axes spanning rows or columns, or mosaic layouts. Simpler figures where a spacing adjustment is sufficient.
Padding controls h_pad and w_pad are in inches; hspace and wspace are fractions of figure size. It also supports a normalized rect and a compress option. pad, h_pad, and w_pad are fractions of the font size; rect is a normalized rectangle for the subplot area.
Engine status The more modern built-in layout engine; generally gives better results, according to the Matplotlib layout-engine API. Matplotlib’s first layout engine, as described in the layout-engine API.

Matplotlib’s constrained-layout guide describes it as similar to tight layout but substantially more flexible. For simple fixed-aspect grids, constrained layout’s compressed option can reduce excess whitespace.

Use constrained layout for a new figure

Set the layout while creating the figure, before adding axes:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2, layout="constrained")

This is especially useful when the figure’s axes have associated colorbars, span multiple rows or columns, or sit in nested subfigures or a mosaic. The engine also tries to align spines across shared rows or columns. The guide documents an alternative global setting, rcParams['figure.constrained_layout.use'] = True.

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

Use tight layout for a straightforward spacing adjustment

For an existing simple figure, call fig.tight_layout() to adjust padding between and around subplots:

fig.tight_layout()

The Figure.tight_layout API defines pad, h_pad, and w_pad as fractions of the font size, and rect as the normalized rectangle the subplot area should fit within. If an Axes artist such as a legend or annotation should not affect the bounding-box calculation, set artist.set_in_layout(False).

What to check when the layout still looks wrong

  • Neither method guarantees correct placement for every custom artist. Constrained layout accounts for tick labels, axis labels, titles, and legends, but other artists can still clip or overlap.
  • Artists positioned in Axes coordinates beyond the Axes boundary may produce unusual results. The constrained-layout guide suggests adding such an artist directly to the Figure.
  • Different row and column geometries made with pyplot.subplot can produce poor constrained-layout results.
  • Font rendering differences between backends can cause small output differences. Inspect the rendered figure in the backend and format that matter to you.
  • On backends with a toolbar, constrained layout is turned off for toolbar zoom and pan events.

Keep constrained layout stable after drawing

Constrained layout normally updates Axes positions on each draw. If positions should remain fixed after an initial draw—for example, because tick labels change during an animation—the guide shows disabling further updates with fig.set_layout_engine('none').

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

Do not combine the two methods

Calling tight_layout() after enabling constrained layout turns constrained layout off, as the official guide explicitly warns. Choose one approach for a figure rather than using tight layout as a final step on a constrained figure.

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.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.