Getting empty tick labels before showing a plot in Matplotlib

To get empty tick labels before showing a plot in Matplotlib, you can access minor tick labels which are typically empty by default. This is useful when you need to check the state of tick labels or manipulate them programmatically.

Steps to Get Empty Tick Labels

  • Create data points for the plot

  • Add a subplot using subplot() method

  • Set major ticks and tick labels using set_xticks() and set_xticklabels()

  • Use get_xticklabels(which='minor') to retrieve empty minor tick labels

  • Display the plot using show() method

Example

Here's how to get empty tick labels and display them before showing the plot ?

from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

x = [1, 2, 3, 4]
ax1 = plt.subplot()
ax1.set_xticks(x)
ax1.set_xticklabels(["one", "two", "three", "four"])

print("Empty tick labels: ", ax1.get_xticklabels(which='minor'))
plt.show()

The output shows an empty list for minor tick labels ?

Empty tick labels:  []

Understanding Minor vs Major Ticks

Here's a comparison of major and minor tick labels ?

from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

x = [1, 2, 3, 4]
ax1 = plt.subplot()
ax1.set_xticks(x)
ax1.set_xticklabels(["one", "two", "three", "four"])

print("Major tick labels:", [label.get_text() for label in ax1.get_xticklabels(which='major')])
print("Minor tick labels:", ax1.get_xticklabels(which='minor'))

plt.show()
Major tick labels: ['one', 'two', 'three', 'four']
Minor tick labels: []

Working with Minor Tick Labels

You can also set and retrieve minor tick labels when needed ?

from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

x = [1, 2, 3, 4]
minor_ticks = [1.5, 2.5, 3.5]

ax1 = plt.subplot()
ax1.set_xticks(x)
ax1.set_xticklabels(["one", "two", "three", "four"])

# Set minor ticks
ax1.set_xticks(minor_ticks, minor=True)

print("Before setting minor labels:", ax1.get_xticklabels(which='minor'))

# Now minor tick labels exist but are empty by default
ax1.set_xticklabels(["", "", ""], minor=True)
print("After setting empty minor labels:", len(ax1.get_xticklabels(which='minor')))

plt.show()
Before setting minor labels: []
After setting empty minor labels: 3

Conclusion

Use get_xticklabels(which='minor') to retrieve empty minor tick labels in Matplotlib. Minor tick labels are empty by default, making this useful for checking label states before customization.

Updated on: 2026-03-25T20:10:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements