Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

python - Getting the coordinates of a Matplotlib annotation label in figure coordinates

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?

Example:

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

enter image description here

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

gives:

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

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

gives:

[[-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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Therefore it is necessary to draw the figure first and then access the bounding box.

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

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

If instead, axes coordinates are desired, it would be

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

or if data coordinates are needed,

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


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...