matplotlib
data visualization
axis removal
legend removal
plot customization

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.

python
1import matplotlib.pyplot as plt
2
3x = [0, 1, 2, 3]
4y = [1, 4, 2, 5]
5
6fig, ax = plt.subplots()
7ax.plot(x, y, color="steelblue", linewidth=2)
8ax.axis("off")
9
10plt.show()

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.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([1, 2, 3], [2, 5, 4], label="Series A")
5
6ax.set_xticks([])
7ax.set_yticks([])
8ax.spines["top"].set_visible(False)
9ax.spines["right"].set_visible(False)
10ax.spines["left"].set_visible(False)
11ax.spines["bottom"].set_visible(False)
12
13plt.show()

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.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([1, 2, 3], [3, 1, 4], label="Signal")
5ax.legend()
6
7legend = ax.get_legend()
8if legend is not None:
9    legend.remove()
10
11plt.show()

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.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots(figsize=(4, 2))
4ax.plot([0, 1, 2], [1, 3, 2], color="black")
5ax.axis("off")
6
7plt.savefig("plot.png", bbox_inches="tight", pad_inches=0, transparent=True)

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:

  1. draw the plot
  2. remove axes or spines
  3. remove the legend if it exists
  4. 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 for None raises 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" and pad_inches=0 for 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" and pad_inches=0.
  • Clean Matplotlib exports come from combining artist cleanup with correct save settings.

Course illustration
Course illustration

All Rights Reserved.