How to Hide Axis Text Ticks and/or Tick Labels in Matplotlib

  1. xaxis.set_visible(False)/yaxis.set_visible(False) to Hide Axis Including Axis label
  2. xaxis.set_ticks([])/yaxis.set_ticks([]) to Hide Axis in Matplotlib
  3. xaxis.set_ticklabels([])/yaxis.set_ticklabels([]) to Hide Axis Label / Text in Matplotlib
  4. 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 color of ticks is set to be white, it could also make the axis text invisible, only if the foreground color of the Matplotlib figure is white.

Matplotlib Normal Axis

xaxis.set_visible(False)/yaxis.set_visible(False) to Hide 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()

Matplotlib Axis_axis_set_visible

xaxis.set_ticks([])/yaxis.set_ticks([]) to Hide Axis in Matplotlib

x/yaxis.set_ticks([]) sets the ticks to be 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()

Matplotlib Axis_axis_set_ticks

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()

Matplotlib Axis_axis_set_ticklabels

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 invisible 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()

Related Article - Matplotlib Axis

  • How to Rotate X-axis Tick Label Text in Matplotlib
  • How to Set Tick Labels Font Size in Matplotlib
  • Related Article - Matplotlib Ticks

  • How to Rotate X-axis Tick Label Text in Matplotlib
  • How to Set Tick Labels Font Size in Matplotlib
  • comments powered by Disqus