How to remove axis, legends, and white padding
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you want a plot to behave like a clean graphic rather than a chart, the default Matplotlib decorations usually get in the way. Removing axes, legends, and extra padding is mostly about using the right artist methods and saving the figure with tight bounding settings.
Remove the Entire Axis Frame
If you want only the plotted content, the fastest option is to disable the axis completely.
ax.axis("off") hides ticks, labels, spines, and the frame in one call. This is ideal for sparkline-like graphics, overlays, or saved assets that will be placed into another layout.
Hide Only Specific Axis Parts
Sometimes you want to keep the data region but remove only the visual clutter. In that case, hide individual components instead of the entire axis.
This approach is useful when you still want manual annotations or other artists to remain positioned inside a normal axes object.
Remove the Legend Safely
Legends can be removed after they are created. The cleanest pattern is to check whether a legend exists before trying to remove it.
That guard matters because calling .remove() on a missing legend raises an error. If you know the legend should never appear, the simpler option is not to call ax.legend() in the first place.
Save Without White Padding
The extra white border usually comes from the default save bounding box, not from the line plot itself. Use bbox_inches="tight" and pad_inches=0 when saving.
bbox_inches="tight" trims unused figure space, and pad_inches=0 removes the remaining margin. transparent=True is optional, but it is helpful when the image will be placed on top of another background.
Putting It Together
For many export workflows, the practical sequence is:
- draw the plot
- remove axes or spines
- remove the legend if it exists
- save with a tight bounding box
If your output still has unexpected whitespace, check for titles, annotations, or figure-level artists that extend beyond the data region. Tight bounding works on everything that belongs to the figure, so hidden extras can still affect the saved bounds.
Common Pitfalls
- Calling
ax.get_legend().remove()without checking forNoneraises an error when no legend was created. Guard the call or skip legend creation entirely. - Hiding only ticks does not remove spines or labels. Use
ax.axis("off")or disable each spine explicitly if you need a fully clean frame. - Saving with default settings preserves surrounding figure whitespace. Use
bbox_inches="tight"andpad_inches=0for trimmed output. - Forgetting that titles and annotations affect the bounding box can leave unexpected margins. Remove or reposition those artists before export.
- Using
plt.show()before saving in some environments can reset or alter state. Save first when you care about reproducible exported output.
Summary
- '
ax.axis("off")removes the full axis decoration in one step.' - Individual ticks, spines, and labels can also be hidden separately for finer control.
- A legend should be removed only if it exists.
- White padding is usually fixed at save time with
bbox_inches="tight"andpad_inches=0. - Clean Matplotlib exports come from combining artist cleanup with correct save settings.

