Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove a frame without removing the axes tick labels from a Matplotlib figure in Python?
To remove a frame without removing the axes tick labels from a Matplotlib figure, you can hide the spines (frame borders) while keeping the tick labels visible. This creates a clean plot appearance while maintaining readability.
Basic Steps
The process involves the following steps ?
- Set the figure size and adjust the padding between and around the subplots
- Create data points for plotting
- Plot the data using
plot()method - Use
set_visible(False)to hide specific spines (frame borders) - Display the figure using
show()method
Example
Here's how to remove the frame while keeping tick labels ?
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
y = [0, 2, 1, 5, 1, 2, 0]
plt.plot(y, color='red', lw=7)
# Remove all four spines (frame borders)
for pos in ['right', 'top', 'bottom', 'left']:
plt.gca().spines[pos].set_visible(False)
plt.show()
Removing Specific Spines Only
You can also remove only certain spines while keeping others visible ?
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True y = [0, 2, 1, 5, 1, 2, 0] plt.plot(y, color='blue', lw=5) # Remove only top and right spines plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.show()
How It Works
The spines property controls the frame borders around the plot. By setting set_visible(False), you hide the visual frame while the axes and tick labels remain intact. The gca() function gets the current axes object, allowing you to modify spine properties.
Conclusion
Use spines[position].set_visible(False) to remove frame borders while preserving tick labels. This technique creates cleaner plots by removing visual clutter while maintaining data readability.
