How to hide ticks label in Python but keep the ticks in place?

When working with matplotlib plots, you might need to hide tick labels while keeping the tick marks visible. This is useful for creating cleaner visualizations or when labels would be redundant or cluttered.

Basic Approach

The simplest method is using plt.xticks() or plt.yticks() with empty labels ?

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
x = np.linspace(1, 10, 100)
y = np.log(x)

# Create the plot
plt.figure(figsize=(8, 4))
plt.plot(x, y, 'b-', linewidth=2)

# Hide x-axis labels but keep ticks
plt.xticks(ticks=range(1, 11), labels=[])

# Add title and labels for clarity
plt.title('Log Function with Hidden X-axis Labels')
plt.ylabel('log(x)')
plt.grid(True, alpha=0.3)
plt.show()

Using tick_params() Method

An alternative approach using tick_params() for more control ?

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

plt.figure(figsize=(8, 4))
plt.plot(x, y, 'r-', linewidth=2)

# Hide labels but keep ticks using tick_params
plt.tick_params(axis='x', labelbottom=False)

plt.title('Sine Wave with Hidden X-axis Labels')
plt.ylabel('sin(x)')
plt.grid(True, alpha=0.3)
plt.show()

Hiding Both X and Y Labels

You can hide labels on both axes simultaneously ?

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
x = np.linspace(-5, 5, 100)
y = x**2

plt.figure(figsize=(8, 6))
plt.plot(x, y, 'g-', linewidth=2)

# Hide both x and y axis labels
plt.xticks(labels=[])
plt.yticks(labels=[])

plt.title('Parabola with No Axis Labels')
plt.grid(True, alpha=0.3)
plt.show()

Comparison of Methods

Method Syntax Best For
xticks(labels=[]) plt.xticks(labels=[]) Quick and simple cases
tick_params() plt.tick_params(axis='x', labelbottom=False) More control and flexibility
set_xticklabels([]) ax.set_xticklabels([]) Object-oriented approach

Key Points

  • Use labels=[] parameter to hide labels while preserving tick marks

  • tick_params() offers more granular control over tick appearance

  • Both methods maintain the tick marks and grid alignment

  • This technique is useful for creating cleaner plots or custom labeling

Conclusion

Hiding tick labels while keeping ticks visible is easily achieved using plt.xticks(labels=[]) or plt.tick_params(). Choose xticks() for simplicity or tick_params() for advanced customization.

Updated on: 2026-03-25T18:32:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements