How to hide ticks label in python but keep the ticks in place?
Asked Answered
M

5

66

I want to hide my ticks label on a plot I created, but keep this tick itself (the little marks on the axis). When I try to use what I've found here, for example, the entire tick is removed, and not just the labels. How can I remove only the labels then?

Mainis answered 5/1, 2014 at 17:22 Comment(0)
S
20

Set the tick labels not to be an empty array, but to be a list of empty strings. In the example below, I've replaced the xtick labels with empty strings, leaving the y axis untouched. From here you can selectively keep certain labels if you wished.

import pylab as plt

fig, ax = plt.subplots()
ax.plot([1,2,3],[4,5,6])

labels = [item.get_text() for item in ax.get_xticklabels()]

empty_string_labels = ['']*len(labels)
ax.set_xticklabels(empty_string_labels)

plt.show()

enter image description here

This code is adapted from a previous answer.

Stocky answered 5/1, 2014 at 18:0 Comment(1)
you can do ax.set_yticklabels([]) without having to initialise a list of empty strings.Eulogist
T
136

Here is a slightly simpler answer, using ax.tick_params

import matplotlib.pylab as plt

fig, ax = plt.subplots()
plt.plot([1,2,3],[4,5,6])

ax.tick_params(labelbottom=False)    

plt.show()

Here is the resulting output in Matplotlib 3

enter image description here

As commented by @chris, one can similarly hide the labels on any of the other axes using labeltop, labelleft, labelright, in the above example, instead of labelbottom.

Thermostatics answered 17/10, 2014 at 15:50 Comment(3)
note for those arriving here to find out how to hide labels on other axes, ax.tick_params accepts the following keyword args: labelbottom, labeltop, labelleft, labelrightHolbrooke
Better than the accepted answer, IMO.Carbolated
Also prefer this method, as the ax.set_xticklabels([]) method also disables the get_major_formatter().get_offset()Granlund
S
20

Set the tick labels not to be an empty array, but to be a list of empty strings. In the example below, I've replaced the xtick labels with empty strings, leaving the y axis untouched. From here you can selectively keep certain labels if you wished.

import pylab as plt

fig, ax = plt.subplots()
ax.plot([1,2,3],[4,5,6])

labels = [item.get_text() for item in ax.get_xticklabels()]

empty_string_labels = ['']*len(labels)
ax.set_xticklabels(empty_string_labels)

plt.show()

enter image description here

This code is adapted from a previous answer.

Stocky answered 5/1, 2014 at 18:0 Comment(1)
you can do ax.set_yticklabels([]) without having to initialise a list of empty strings.Eulogist
F
7

I didn't find divenex's answer to work for me. This answer creates ticks without triggering any automatic labeling, hence no requirement to use "labelbottom= False":

if the bottom and left ticks already exist:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plt.plot([1,2,3],[4,5,6])

ax.tick_params(right= True,top= True)

if no ticks already exist:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plt.plot([1,2,3],[4,5,6])

ax.tick_params(right= True,top= True,left= True, bottom= True)
Featly answered 10/9, 2018 at 8:49 Comment(1)
This answer does not hide the labels. It creates the ticks, leaving the labels unchanged.Thermostatics
A
3
import matplotlib.pylab as plt

fig, ax = plt.subplots()
plt.plot([1,2,3],[4,5,6])

ax.set_xticklabels([])

plt.show()
Anatomize answered 15/1, 2022 at 21:3 Comment(1)
the most neat answer for this!Corsica
C
0

matplotlib.ticker.NullFormatter seems to be a cleaner way to hide labels.

from matplotlib import ticker

# hide labels in minor ticks
ax.xaxis.set_minor_formatter(ticker.NullFormatter())
# hide labels in major ticks
ax.xaxis.set_major_formatter(ticker.NullFormatter())
Charlet answered 6/6 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.