Open In App

How to Hide Axis Text Ticks or Tick Labels in Matplotlib?

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Matplotlib library in python shows axis ticks and tick labels by default. Sometimes it is important to hide these axis ticks and tick labels. In this article we will discuss some methods by which this can be done. Before that lets have a understanding about each of them:

  • Ticks: Axes’ points are marked with ticks, which are also known as small geometrical scale lines.
  • Tick labels: They are the name given to the ticks or we can say, tick labels are text-filled ticks known as Text Ticks.
  • Axes: X-axis and Y-axis are referred to as axis labels.

We are having different methods for this, some of them are explained below:

Method 1: Remove tick labels by setting them to an Empty List

The xticks() and yticks() functions in Matplotlib are used to define the positions of the ticks on x and y axes respectively. These functions take a list of values that represent tick locations. By setting tick labels to an empty list we can hide ticks and their corresponding labels as shown below:

plt.xticks([])
plt.yticks([])

Python
import matplotlib.pyplot as plt
x1 = [5, 8, 10]
y1 = [12, 16, 8]
x2 = [6, 9, 12]
y2 = [14, 10, 8]
plt.plot(x1, y1, 'g', linewidth=7)
plt.plot(x2, y2, 'b', linewidth=7)
plt.xticks([]) 
plt.yticks([]) 
plt.show()

Output

hide-axis11

Tick labels set to empty 

Method 2: Hide Tick Labels by setting their color to White

In Matplotlib by default background color is white. By setting the color of the tick labels to white we can effectively hide them since they blend with the background. For this color attribute needs to pass with w (represents white) as a value to xticks() and yticks() function.

Python
import matplotlib.pyplot as plt
plt.plot([5, 10, 20], [20, 10, 50], color='g')
plt.xlabel("X Label")
plt.ylabel("Y Label")
plt.xticks(color='w')
plt.yticks(color='w')
plt.show()

Output

hide-axis-2

By setting the color white

Method 3: Hide Ticks and Labels by using NullLocator()

A null Locator is a type of tick locator that makes axis ticks and tick labels disappear. Simply passing NullLocator() function will be enough.

  • np.random.rand(100): Creates an array with 100 random values where each value is a float between 0 and 1.
  • ax.xaxis.set_major_locator(ticker.NullLocator()): This modifies x-axis of the plot by setting major tick locator to NullLocator().
  • ax.yaxis.set_major_locator(ticker.NullLocator()): This is similar to the previous one but modifies y-axis.
Python
import numpy as np
import matplotlib.ticker as ticker
ax = plt.axes()
x = np.random.rand(100)
ax.plot(x, color='g')
ax.xaxis.set_major_locator(ticker.NullLocator())
ax.yaxis.set_major_locator(ticker.NullLocator())

Output

hide-axis-3

Using NullLocator()

Method 4: Hide Tick Labels by placing Blank space

We can hide tick labels by passing a blank space (" ") as tick label. This will hide labels but keep ticks visible on the plot.

Python
import matplotlib.pyplot as plt
x = [5, 8, 15, 20, 30]
y = [15, 10, 8, 20, 15]
plt.plot(x, y, color='g', linewidth=5)
plt.xticks(x, "")
plt.yticks(y, "")
plt.show()

Output

hide-axis-4

Placing blank space

Method 5: Hide Ticks and Labels Using set_visible(False)

Using set_visibile() we can set visibility of tick labels as False, which will not make them appear in our plot. This method hides labels and ticks both.

Python
import matplotlib.pyplot as plt
x = [5, 8, 15, 20, 30]
y = [15, 10, 8, 20, 15]
plt.plot(x, y, color='g', linewidth=5)
a = plt.gca()
xax = a.axes.get_xaxis()
xax = xax.set_visible(False)
yax = a.axes.get_yaxis()
yax = yax.set_visible(False)
plt.show()

Output

hide-axis-6

By setting set_visible(False)

By using these methods we can easily customize our Matplotlib plots enhancing their clarity and focusing on the most important data points.



Next Article

Similar Reads