How to hide the axes spines of a plot with Matplotlib using Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Removing the axes spine can improve readability. Especially when you want to include such graph in a presentation.
Matplotlib will provide you with a .set_visible() method to apply on the spines attribute of an ax object.
You can apply the method .set_visible(False) to any spines of the ax you want and it will hide it.
Here is the code
# We import our libraries
import matplotlib.pyplot as plt
import numpy as np
# Generate our sample dataset
x = np.linspace(1,100)
y = x ** 2
# We create a canvas with one ax
fig, axes = plt.subplots(1, 1, figsize=(8,4))
# We plot on our first axis
axes.plot(x, y, alpha=0.6, c='tab:blue')
# Set a title
axes.set_title("Hidden axis spines")
# Hide the right and top spines
axes.spines['right'].set_visible(False)
axes.spines['top'].set_visible(False)
# We tight the layout
fig.tight_layout()
# We plot
plt.show()
Here is the result
Here you are! You now know how to hide the axes spines of a plot with Matplotlib using Python.
More on Matplotlib
If you like what you've just read and want to know more about the Matplotlib library (e.g. how to add labels, plot different types of plots, etc...) check out the other articles I wrote on the topic, just here :
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚