Python matplotlib,获取xtick标签的位置 [英] Python matplotlib, get position of xtick labels

查看:26
本文介绍了Python matplotlib,获取xtick标签的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得 xtick 主要标签的位置?我从 label.get_position() 得到的值没有意义.

将 numpy 导入为 np导入matplotlib.pyplot作为plt定义 f(t):返回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)无花果,ax = plt.subplots()plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')#plt.show()打印(图)打印(ax.get_position())#------------------------------------------------刻度标签的#个位置,返回的错误(0,0)#------------------------------------------------打印([text.get_position() 用于 ax.get_xticklabels()] 中的文本# 正确的刻度标签值打印(ax.get_xticks())

以上代码的输出为:

图(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)]<-错误的位置[0. 1. 2. 3. 4. 4. 5.]

我如何获得 xtick 主要标签的位置?我从label.get_position()获取的值没有意义.有没有我不知道的转换?最终我想要以 (x,y) 图像像素为单位的文本框位置.

解决方案

如果需要像素坐标,就需要图形坐标,并对其进行变换.

如果您需要有关转换的更多信息:请查看此matplotlib转换教程: ref

为了完整起见,我添加了指定 dpi 的选项,这将影响您的图形尺寸

 将matplotlib导入为mpl将numpy导入为np导入matplotlib.pyplot作为plt定义 f(t):返回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)# 在最终图形中设置所需的 dpidpi = 300mpl.rcParams ['figure.dpi'] = dpi无花果,ax = plt.subplots()plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')# 保存图形:不要忘记 dpi 选项!fig.savefig('./out.png',format ='png',dpi = dpi)xtickslocs = ax.get_xticks()ymin, _ = ax.get_ylim()print('xticks 像素坐标')打印(ax.transData.transform([(xtick,ymin)xtickslocs中的xtick]))打印(标签边界框")打印([在ax.get_xticklabels()中用于l的[l.get_window_extent()])xticks 像素坐标数组([[ 60. , 40. ],[134.4, 40.],[208.8, 40.],[283.2, 40.],[357.6,40.],[432., 40.]])标签边框[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]])]

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.

解决方案

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]])]

这篇关于Python matplotlib,获取xtick标签的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆