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.
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:
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:
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:
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:
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:
Then use it on every subplot:
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:
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)andax.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
- How can I return pivot table output in MySQL?
- How can I run Tensorboard on a remote server?
- How can I run Tensorboard on a remote server?
- How can I see function arguments in IPython Notebook Server 3?
- How can I rename a conda environment?
- How can I replace or strip an extension from a filename in Python?
- How can I separate runs of my TensorFlow code in TensorBoard?
- How can I separate runs of my TensorFlow code in TensorBoard?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.