Justin Justin - 1 year ago 274
Python Question

Python matplotlib, get position of xtick labels

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.

Answer Source

If you need the pixel coordinates, you need the figure coordinates, and tranform them.

If you need more information on transformations: check this matplotlib transformation tutorial: ref

EDIT: for completeness, I added the option to specify the dpi, which will influence your figure dimensions

import matplotlib as mpl
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)

# set the dpi you want in your final figure
dpi = 300
mpl.rcParams['figure.dpi'] = dpi

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

# saving the figure: don't forget the dpi option!
fig.savefig('./out.png', format='png', dpi=dpi)

xtickslocs = ax.get_xticks()
ymin, _ = ax.get_ylim()
print('xticks pixel coordinates')
print(ax.transData.transform([(xtick, ymin) for xtick in xtickslocs]))
print('label bounding boxes')
print([l.get_window_extent() for l in ax.get_xticklabels()])

xticks pixel coordinates
array([[  60. ,   40. ],
       [ 134.4,   40. ],
       [ 208.8,   40. ],
       [ 283.2,   40. ],
       [ 357.6,   40. ],
       [ 432. ,   40. ]])
label bounding boxes
[Bbox([[56.4375, 25.5555555556], [63.5625, 35.5555555556]]),
 Bbox([[130.8375, 25.5555555556], [137.9625, 35.5555555556]]),
 Bbox([[205.2375, 25.5555555556], [212.3625, 35.5555555556]]),
 Bbox([[279.6375, 25.5555555556], [286.7625, 35.5555555556]]),
 Bbox([[354.0375, 25.5555555556], [361.1625, 35.5555555556]]),
 Bbox([[428.4375, 25.5555555556], [435.5625, 35.5555555556]])]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download