Matplotlib Saved Figures Cut Off Labels: Diagnostic Guide
Diagnose why Matplotlib saves PNG or PDF files that clip titles, axis labels, or legends while the on-screen plot looks complete.
30 Aug 2025, 20:05 UTC

Recognizable condition
You run a Matplotlib script, the plot looks complete on screen, but the saved PNG or PDF cuts off the x-axis label, clips the legend, or trims the title. The interactive figure appears fine, yet the exported file does not match what you saw.
Useful takeaway
Most clipping problems come from one of three settings: the savefig() bounding box, the active backend, or the order of layout calls. Check the backend first, then save with bbox_inches='tight', and call tight_layout() after every artist is added but before savefig().
Symptom and cause table
| Symptom | Likely cause | Fast check |
|---|---|---|
| Saved image cuts off title, axis label, or legend | savefig() defaults to bbox_inches=None, clipping to the axes bounding box | Resave with bbox_inches='tight' and compare |
| Blank or wrong-sized file on a headless server | GUI backend such as TkAgg or QtAgg with no display | Run matplotlib.get_backend() |
| Overlapping labels or clipped annotations | tight_layout() called before all artists exist, or not called at all | Confirm layout runs after plotting and before saving |
Output pixels do not match figsize times dpi | bbox_inches='tight' resizes the canvas, or backend scaling differs | Multiply fig.get_size_inches() by dpi |
Ordered checks
- Confirm the backend. Run in a terminal:
Where: terminal or console. Required permissions: none. Expected output: a backend name such aspython -c 'import matplotlib; print(matplotlib.get_backend())'Agg,TkAgg, orQtAgg. Risk: importingmatplotlib.pyplotbefore this check in the same session can lock in a backend and make the result misleading. - Inspect figure size and resolution.
Expected:import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(6, 4)) print(fig.get_size_inches()) print(fig.dpi)[6. 4]and100by default. This establishes the baseline pixel size beforebbox_inches='tight'changes it. - Force a canvas draw.
Run this after plotting and before saving so all artists are included in bounding-box calculations.fig.canvas.draw() - Compare saves with and without tight bounding box.
Inspect both files. Iffig.savefig('clipped.png', dpi=100) fig.savefig('included.png', bbox_inches='tight', dpi=100)included.pngshows labels or legends thatclipped.pngcuts off, the default bounding box was the cause.
Fixes matched to each finding
Clipped labels, titles, or legends
Use bbox_inches='tight' so Matplotlib expands the saved canvas to include artists outside the axes box. If a legend or annotation is placed with bbox_to_anchor and still gets cut off, pass it explicitly:
fig.savefig(
'output.png',
bbox_inches='tight',
bbox_extra_artists=[ax.legend(), ax.title],
dpi=100
)
Where: Python script, immediately before saving. Required permissions: none, only write access to the output directory. Risk: bbox_inches='tight' changes output dimensions and can crop deliberately placed artists unless they are listed in bbox_extra_artists.
Wrong or blank output on a headless system
Set the non-interactive Agg backend before importing pyplot:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
Where: first lines of your Python script or notebook cell. Required permissions: none. Risk: this has no effect if matplotlib.pyplot was already imported earlier in the session.
Overlaps or layout clipping
Call fig.tight_layout() after all plotting commands and before savefig(). Do not call it after saving; it cannot retroactively change the file.
ax.plot([1, 2, 3], [1, 4, 9])
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_title('Sample plot')
fig.tight_layout()
fig.savefig('output.png', bbox_inches='tight', dpi=100)
Close figures in long-running scripts to avoid memory buildup:
plt.close(fig)
When to escalate
Escalate if the same minimal script fails on Agg with bbox_inches='tight' across Matplotlib versions. When reporting the issue, include Python version, Matplotlib version, active backend, and a self-contained code sample that reproduces the clipping.
Limitations and practical verification
bbox_inches='tight' can change output dimensions and may exclude intentionally placed artists unless they are added to bbox_extra_artists. Screen rendering and file rendering can differ even with identical code, and tight_layout() does not automatically account for artists placed outside the axes with bbox_to_anchor.
Verify the result by opening the saved PNG or PDF and comparing its edges against the on-screen figure. For a numeric check, multiply fig.get_size_inches() by dpi to predict the pixel dimensions when bbox_inches is not 'tight', then compare with the actual image dimensions using an image tool or Python imaging library.
Saving a file overwrites an existing file with the same name but does not change persistent system state, so no rollback procedure is needed. Save to a test filename first to avoid clobbering an important output.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.