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

Jinku Hu Feb 02, 2024
  1. xaxis.set_visible(False)/yaxis.set_visible(False) to Hide Matplotlib 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
How to Hide Axis Text Ticks and/or Tick Labels 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.

Matplotlib Normal Axis

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

Matplotlib Axis_axis_set_visible

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

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 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()
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

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

Related Article - Matplotlib Axis

Related Article - Matplotlib Ticks