Python matplotlib, get position of xtick labels

Viewed 19657

How do I get the positions of the xtick major labels? The values that I am getting from label.get_position() do not make sense.

import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

# fig, ax = plt.figure(1)
fig, ax = plt.subplots()
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

# plt.show()
print(fig)
print(ax.get_position())

# ------------------------------------------------
# positions of the tick labels, incorrect (0,0) returned
# ------------------------------------------------
print([text.get_position() for text in ax.get_xticklabels()])
# correct tick label values
print(ax.get_xticks())

Output from the above code is:

Figure(640x480)
Bbox('array([[ 0.125,  0.1  ],\n       [ 0.9  ,  0.9  ]])')
[(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0)] <-- incorrect positions
[ 0.  1.  2.  3.  4.  5.]

How do I get the positions of the xtick major labels? The values that I am getting from label.get_position() do not make sense. Is there a transform that I don't know about? Ultimately I want the position of the text boxes in (x,y) image pixel units.

2 Answers
Related