Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 380
xnx Avatar asked Dec 14 '22 01:12

xnx


1 Answers

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.

like image 189
ImportanceOfBeingErnest Avatar answered Dec 23 '22 09:12

ImportanceOfBeingErnest