get_xticklabels() contains empty text instances

I will answer according to the title in your question because I don’t understand the explanation that follows it.

The tick labels are not populated until the figure is drawn.

plt.plot([1, 2])
ax = plt.gca()
labels = ax.get_xticklabels()
for label in labels:
    print(label)

Output:

Text(0,0,'')
Text(0,0,'')
Text(0,0,'')
Text(0,0,'')
Text(0,0,'')
Text(0,0,'')
Text(0,0,'')
Text(0,0,'')

When you call plt.draw() the tick labels are populated:

plt.plot([1, 2])
ax = plt.gca()
plt.draw()
labels = ax.get_xticklabels()
for label in labels:
    print(label)

Output:

Text(0,0,'')
Text(0,0,'0.0')
Text(0.2,0,'0.2')
Text(0.4,0,'0.4')
Text(0.6,0,'0.6')
Text(0.8,0,'0.8')
Text(1,0,'1.0')
Text(0,0,'')

Leave a Comment