获取 Matplotlib 绘制标签坐标 [英] Get Matplotlib plots labels coordinates

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

问题描述

我想访问图中的所有标签坐标.

I would like to access to all labels coordinates in a plot.

例如,我画两条线并显示一个图例:

For example, I draw 2 lines and show a legend:

import matplotlib.pyplot as plt
plt.plot([1,2],label="first_image")
plt.plot([2,1],label="second_image")
plt.legend()
plot.show()

我想获取下图中所有带圆圈的标签的位置:

I would like to get the position of all the circled labels in the following image:

推荐答案

您可以使用 get_window_extent()方法来获取艺术家在窗口坐标中的大部分位置.

You can get most of the artists' positions in window coordinates by using the get_window_extent() method.

为了能够使用这种方法,艺术家需要事先被绘制到画布上.可以通过 plt.gcf().canvas.draw()手动触发.

To be able to use this method, the artist needs to have previously been drawn to the canvas. This can be manually triggered via plt.gcf().canvas.draw().

import matplotlib.pyplot as plt
plt.plot([1,2],label="first_image")
plt.plot([2,1],label="second_image")
leg = plt.legend()

plt.gcf().canvas.draw()
ticks = [t for t in plt.gca().get_xticklabels()]
for i, t in enumerate(ticks):
    print "Label {}, data: {}".format(i, t.get_text()), t.get_window_extent()
print "Legend location: ", leg.get_window_extent()
for i, l in enumerate(leg.texts):
    print "Label {}, {}".format(i, l.get_text()), l.get_window_extent()
plt.show()

这将打印所有相关坐标

Label 0, data:  Bbox(x0=102.545454545, y0=43.0777777778, x1=102.545454545, y1=43.0777777778)
Label 1, data: 0.0 Bbox(x0=91.6079545455, y0=29.0777777778, x1=113.482954545, y1=43.0777777778)
Label 2, data: 0.2 Bbox(x0=181.789772727, y0=29.0777777778, x1=203.664772727, y1=43.0777777778)
Label 3, data: 0.4 Bbox(x0=271.971590909, y0=29.0777777778, x1=293.846590909, y1=43.0777777778)
Label 4, data: 0.6 Bbox(x0=362.090909091, y0=29.0777777778, x1=384.090909091, y1=43.0777777778)
Label 5, data: 0.8 Bbox(x0=452.272727273, y0=29.0777777778, x1=474.272727273, y1=43.0777777778)
Label 6, data: 1.0 Bbox(x0=542.454545455, y0=29.0777777778, x1=564.454545455, y1=43.0777777778)
Label 7, data:  Bbox(x0=102.545454545, y0=43.0777777778, x1=102.545454545, y1=43.0777777778)
Legend location:  Bbox(x0=419.305555556, y0=214.431597222, x1=569.055555556, y1=260.768402778)
Label 0, first_image Bbox(x0=463.75, y0=241.072222222, x1=541.375, y1=255.212847222)
Label 1, second_image Bbox(x0=463.75, y0=219.987152778, x1=563.5, y1=234.127777778)

但是请注意,通常不需要或不建议将这些坐标用于图形中的任何操作,因为每次重新绘制时它们都可能会改变.根据使用情况,可能有更好的方法来实现某个目标.

Note however that it is in general not required or recommended to use those coordinates for any manipulations inside the figure, since they may change for every redraw. Depending on the usage case there might be better methods to achieve a certain goal.

另请注意,这些坐标不一定是保存图像中的像素坐标.这些将取决于 dpi 设置,在屏幕上和保存的图像之间可能会有所不同.此外,用于保存的后端可能会重新绘制可能会更改坐标的画布.

Also note that those coordinates are not necessarily the pixel coordinates in the saved image. Those will depend on the dpi setting which may be different between on-screen and saved image. Also the backend used for saving may redraw the canvas which can possibly change the coordinates.

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

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