tight_layout() (Matplotlib)

fig.tight_layout() automatically adjusts the spacing around and between subplots so that titles, axis labels, and tick labels don't overlap or get clipped. It fixes the common 'labels cut off' problem.

Matplotlib

When you have several subplots or long labels, the default spacing often lets text overlap neighboring panels or run off the edge of the saved image. Calling fig.tight_layout() recomputes the margins and gaps to give every element room, usually right before showing or saving.

The pad argument controls the overall padding, and w_pad/h_pad fine-tune the horizontal and vertical gaps between panels. It works by inspecting the current sizes of the text, so call it after you have added all titles and labels.

A newer, often better alternative is creating the figure with constrained_layout=True (or layout='constrained' in subplots), which adjusts spacing continuously as the figure is built rather than once at the end. Both solve the same overlap problem.

Syntax

fig.tight_layout(pad=1.08)  |  plt.tight_layout()

Examples

Python — Re-spaces the 4 panels so labels don't collide.

import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)
fig.tight_layout()
print("adjusted")

Output:

adjusted

Learn it in a lesson

See also

Back to the Dictionary