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 DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

Matplotlib `tight_layout`, `wspace` and `hspace` in Python

Set Matplotlib subplot gaps with subplots_adjust(wspace=..., hspace=...). tight_layout uses font-relative padding controls instead.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

tight_layout() does not take wspace or hspace. To set subplot gaps directly, use subplots_adjust(wspace=..., hspace=...). Use tight_layout() when you want Matplotlib to adjust padding around subplot labels and titles automatically; its spacing controls are pad, w_pad and h_pad.

Set subplot gaps with subplots_adjust

For explicit control over horizontal and vertical gaps, call subplots_adjust on the figure:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)
fig.subplots_adjust(wspace=0.35, hspace=0.45)

You can also use the pyplot form, plt.subplots_adjust(wspace=0.35, hspace=0.45). These example values are starting points, not universal settings. Matplotlib defines wspace as a fraction of the average Axes width and hspace as a fraction of the average Axes height. See the Matplotlib subplots_adjust API.

Use tight_layout for automatic padding

tight_layout() adjusts subplot parameters to make room for labels, tick labels and titles. Its documented arguments include pad, w_pad, h_pad and rect—not wspace or hspace. The padding values are fractions of font size, and w_pad and h_pad default to pad. The optional rect is a tuple of (left, bottom, right, top) coordinates normalized to the 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.
fig.tight_layout(pad=1.1, w_pad=0.8, h_pad=0.8)

The numbers in this example are illustrative. They are not equivalent to the same numeric values passed to subplots_adjust, because the APIs use different units and layout rules. For parameter details, see the Matplotlib tight_layout API.

Choose the layout method that fits your figure

What you need Use How spacing is specified
Directly set gaps between subplots subplots_adjust() wspace and hspace are fractions of average Axes width and height.
Automatically adjust padding for subplot labels and titles tight_layout() pad, w_pad and h_pad are fractions of font size.
Let a layout engine coordinate the figure layout Constrained layout Uses its own spacing and padding controls; these do not share the units of tight_layout.

For constrained layout, enable it when creating a figure with layout="constrained". Its wspace and hspace are distributed among the gaps as fractions of figure space; for example, with three columns, wspace=0.2 allocates 0.1 of figure width to each of the two gaps. The current layout-engine API lists defaults of 0.02 for both spacing settings and 0.04167 inches for each pad. Consult the Matplotlib layout-engine API for the engine’s behavior and controls.

Matplotlib describes constrained layout as more modern than tight layout and generally likely to give better results, but the better choice depends on the figure. The tight-layout guide also cautions that tight layout checks tick-label, axis-label and title extents and may not work in some cases. Neither approach should be assumed to handle every artist or custom layout.

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

Check your Matplotlib version

The linked stable references document Matplotlib 3.11.2 for tight_layout, subplots_adjust and the layout-engine API; the configuration reference is for 3.11.0. If you use another release, check its documentation for the available arguments and defaults.

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.