在图形坐标中获取Matplotlib标注标签的坐标 [英] Getting the coordinates of a Matplotlib annotation label in figure coordinates

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

问题描述

我想知道图形分数坐标中 Matplotlib 图的文本注释的边界矩形的坐标.但是,当我尝试访问与注释关联的补丁的范围"时,无论如何我都会得到 Bbox(x0 = -0.33,y0 = -0.33,x1 = 1.33,y1 = 1.33)文本标签的大小.这些坐标似乎与 IdentityTransform 相关联,但不会转换为任何有意义的图形分数坐标.如何以图形分数单位获取标签的坐标(理想的是,左下角和右上角)?

I'd like to know the coordinates of the bounding rectangle of a text annotation to a Matplotlib plot in figure fraction coordinates. However, when I try to access the "extents" of the patch associated with the annotation, I get Bbox(x0=-0.33, y0=-0.33, x1=1.33, y1=1.33) regardless of the size of the text label. These coordinates seem to be associated with an IdentityTransform, but don't transform into any meaningful figure fraction coordinates. How can I obtain the label's coordinates (ideally, lower left corner and upper right corner) in figure fraction units?

示例:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return 10 * np.sin(3*x)**4

x = np.linspace(0, 2*np.pi, 100)
y = f(x)

fig, ax = plt.subplots()
ax.plot(x,y)

xpt = 1.75
ypt = f(xpt)
xy = ax.transData.transform([xpt, ypt])
xy = fig.transFigure.inverted().transform(xy)

xytext = xy + [0.1, -0.1]
rdx, rdy = 0, 1
ann = ax.annotate('A point', xy=xy, xycoords='figure fraction',
             xytext=xytext, textcoords='figure fraction',
             arrowprops=dict(arrowstyle='->', connectionstyle="arc3",
                             relpos=(rdx, rdy)),
             bbox=dict(fc='gray', edgecolor='k', alpha=0.5),
             ha='left', va='top'
            )

patch = ann.get_bbox_patch()
print(patch.get_extents())

给出:

[[-0.33 -0.33]
 [ 1.33  1.33]]

c = patch.get_transform().transform(patch.get_extents())
print(c)

给予:

[[-211.2 -158.4]
 [ 851.2  638.4]]

大概是显示坐标,但它们与我想要属性的标签的位置和大小不对应.

Presumably these are display coordinates, but they don't correspond to the position and size of the label I want the properties of.

推荐答案

在绘制图形之前, text 对象的边界框仅包含该框相对于内部文本的坐标.

Before the figure is drawn, the bounding box of a text object contains just the coordinates of the box relative to the text inside.

因此需要先绘制图形,然后再访问边界框.

fig.canvas.draw() 
patch = ann.get_bbox_patch()
box  = patch.get_extents()
print box
#prints: Bbox(x0=263.6, y0=191.612085684, x1=320.15, y1=213.412085684)

由于这些是显示单位中框的坐标,因此需要将其转换为图形单位

Since those are the coordinates of the box in display units, they need to be tranformed to figure units

tcbox = fig.transFigure.inverted().transform(box)
print tcbox
#prints [[ 0.411875    0.39919185]
#        [ 0.50023438  0.44460851]]

# The format is 
#        [[ left    bottom]
#         [ right   top   ]]

这将返回文本周围矩形的图形单位(范围从 0 到 1)的边界框.

This returns the bounding box in figure units (ranging from 0 to 1) of the rectangle around the text.

如果需要轴坐标,则为

ax.transAxes.inverted().transform(box)

或者如果需要数据坐标,

or if data coordinates are needed,

ax.transData.inverted().transform(box)

<小时>如果文本本身的边界框是被要求的,则可以使用 matplotlib.text.Textget_window_extent() 方法并提供注释对象作为参数.使用


If instead the bounding box of the text itself is what's beeing asked for one can use the get_window_extent() method of matplotlib.text.Text and supply the annotation object as argument. Using

box = matplotlib.text.Text.get_window_extent(ann)
print box
# prints Bbox(x0=268.0, y0=196.012085684, x1=315.75, y1=209.012085684)

可以按上述方法得到图形单位的框.

one can proceed as above to obtain the box in figure units.

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

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