matplotlib
Python
data visualization
axis removal
coding tutorial

How can I remove the top and right axis?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In Matplotlib, the top and right borders of a plot are controlled by the axes spines. If you want a cleaner chart style, the usual solution is to hide those two spines and optionally move tick marks to the left and bottom only.

Remove the Top and Right Spines

The most direct approach is:

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4y = [2, 5, 3, 6]
5
6fig, ax = plt.subplots()
7ax.plot(x, y)
8
9ax.spines["top"].set_visible(False)
10ax.spines["right"].set_visible(False)
11
12plt.show()

This keeps the data and tick labels intact while removing the extra frame lines.

Clean Up Tick Placement Too

If you hide the top and right spines, it often looks better to keep ticks only on the left and bottom:

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([1, 2, 3], [4, 1, 5])
5
6ax.spines["top"].set_visible(False)
7ax.spines["right"].set_visible(False)
8ax.yaxis.set_ticks_position("left")
9ax.xaxis.set_ticks_position("bottom")
10
11plt.show()

Without this step, some styles may still try to place ticks on now-hidden sides.

Use Seaborn’s Shortcut

If you are already using Seaborn, there is a convenient helper:

python
1import seaborn as sns
2import matplotlib.pyplot as plt
3
4sns.set_theme(style="ticks")
5
6fig, ax = plt.subplots()
7ax.plot([1, 2, 3], [2, 4, 3])
8
9sns.despine(ax=ax)
10plt.show()

By default, sns.despine() removes the top and right spines. It is a nice shortcut for publication-style figures.

You can also control exactly which sides are removed:

python
sns.despine(ax=ax, top=True, right=True, left=False, bottom=False)

Why This Works

In Matplotlib, the visible border lines are not the same thing as the plotted data or the axis scale. They are just decorative spine objects attached to the axes. Hiding them changes the appearance of the frame, not the meaning of the plot.

That distinction is useful because you can simplify the visual presentation without changing the data, labels, or coordinate system.

Global Styling for Many Plots

If you want this appearance across many figures, wrap it in a small helper:

python
1def simplify_axes(ax):
2    ax.spines["top"].set_visible(False)
3    ax.spines["right"].set_visible(False)
4    ax.xaxis.set_ticks_position("bottom")
5    ax.yaxis.set_ticks_position("left")

Then use it on every subplot:

python
1fig, axes = plt.subplots(1, 2, figsize=(8, 3))
2
3for ax in axes:
4    ax.plot([1, 2, 3], [3, 1, 4])
5    simplify_axes(ax)
6
7plt.tight_layout()
8plt.show()

This keeps the style consistent and avoids repeating the same four lines everywhere.

Difference Between Spines and Axes Labels

People sometimes say “remove the top and right axis” when they really mean “remove the border lines.” If you hide the spines, your x-axis and y-axis labels still exist:

python
ax.set_xlabel("Time")
ax.set_ylabel("Value")

So the plot still communicates the scale clearly. You are removing clutter, not removing the axes themselves in a mathematical sense.

Common Pitfalls

The biggest pitfall is hiding spines but forgetting tick placement. The plot may still look odd if ticks are configured for hidden sides.

Another common issue is applying the change to the wrong object. ax.spines[...] works on an Axes instance, not directly on the matplotlib.pyplot module.

When using Seaborn, people also sometimes call sns.despine() before the figure or axes exist. It should run after the plot has been created.

Finally, do not remove too much framing on plots that already have dense content. Minimal styling is helpful, but the chart still needs enough structure for the reader to interpret it quickly.

Summary

  • Hide the top and right sides with ax.spines["top"].set_visible(False) and ax.spines["right"].set_visible(False).
  • For a cleaner result, keep ticks on the left and bottom only.
  • 'sns.despine() is a convenient shortcut when using Seaborn.'
  • Spines are visual borders, so hiding them does not change the data or axis scale.
  • A small helper function makes this style easy to apply consistently.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.