How to Hide Axis Text Ticks and/or Tick Labels in Matplotlib
-
xaxis.set_visible(False)
/yaxis.set_visible(False)
to Hide Matplotlib Axis Including Axislabel
-
xaxis.set_ticks([])
/yaxis.set_ticks([])
to Hide Axis in Matplotlib -
xaxis.set_ticklabels([])
/yaxis.set_ticklabels([])
to Hide Axis Label / Text in Matplotlib -
xticks(color='w')
/yticks(color='w')
to Hide Axis Label / Text in Matplotlib
The plot in Matplotlib by default shows the ticks
and ticklabels
of two axes
as shown in the example figure.
It has different methods to hide the axis text, like xaxis.set_visible(False)
, xaxis.set_ticks([])
and xaxis.set_ticklabels([])
. If the ticks’ color is set to be white, it could also make the axis text invisible, only if the foreground color of the Matplotlib figure is white.
xaxis.set_visible(False)
/yaxis.set_visible(False)
to Hide Matplotlib Axis Including Axis label
As its name suggests, it makes the complete axis invisible, including axis ticks, axis tick labels, and axis label
.
import matplotlib.pyplot as plt
plt.plot([0, 10], [0, 10])
plt.xlabel("X Label")
plt.ylabel("Y Label")
ax = plt.gca()
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
plt.grid(True)
plt.show()
xaxis.set_ticks([])
/yaxis.set_ticks([])
to Hide Axis in Matplotlib
x/yaxis.set_ticks([])
sets the ticks empty and makes the axis ticks and their labels invisible. But the axis label is not influenced.
import matplotlib.pyplot as plt
plt.plot([0, 10], [0, 10])
plt.xlabel("X Label")
plt.ylabel("Y Label")
ax = plt.gca()
ax.axes.xaxis.set_ticks([])
ax.axes.yaxis.set_ticks([])
plt.grid(True)
plt.show()
xaxis.set_ticklabels([])
/yaxis.set_ticklabels([])
to Hide Axis Label / Text in Matplotlib
x/yaxis.set_ticklabels([])
sets the tick labels to be empty so that it makes the axis text (tick labels) invisible but leaves ticks visible.
import matplotlib.pyplot as plt
plt.plot([0, 10], [0, 10])
plt.xlabel("X Label")
plt.ylabel("Y Label")
ax = plt.gca()
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
plt.show()
xticks(color='w')
/yticks(color='w')
to Hide Axis Label / Text in Matplotlib
This tricky method doesn’t make the tick labels or ticks invisible but sets the color of ticks to white so that the axis text is indeed hidden if the background of the plot is white (also default color).
import matplotlib.pyplot as plt
plt.plot([0, 10], [0, 10])
plt.xlabel("X Label")
plt.ylabel("Y Label")
plt.xticks(color="w")
plt.yticks(color="w")
plt.grid(True)
plt.show()
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook