How to hide the axes spines of a plot with Matplotlib using Python

1 min

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

Hidden axis spines plot

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 :

Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.