Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coordinates of box of annotations in matplotlib

How can I get the coordinates of the box displayed in the following plot?

enter image description here

fig, ax = subplots()
x = ax.annotate('text', xy=(0.5, 0), xytext=(0.0,0.7), 
                ha='center', va='bottom',
                bbox=dict(boxstyle='round', fc='gray', alpha=0.5),
                arrowprops=dict(arrowstyle='->', color='blue'))

I tried to inspect the properties of this object, but I couldn't find something suited to this purpose. There is a property called get_bbox_patch() which could be on the right track, however, I get results in a different coordinate system (or associated to a different property)

y = x.get_bbox_patch()
y.get_width()
63.265625

Thanks a lot!

like image 773
Robert Smith Avatar asked Jul 23 '13 02:07

Robert Smith


People also ask

What is the purpose of the XY attribute of the annotate () function?

xy: This parameter is the point (x, y) to annotate. xytext: This parameter is an optional parameter. It is The position (x, y) to place the text at. xycoords: This parameter is also an optional parameter and contains the string value.

Which attribute is used to annotate the arrow position in a plot in Python?

The Axes. annotate() function in axes module of matplotlib library is also used to annotate the point xy with text text.In other word, it i used to placed the text at xy.


1 Answers

ax.figure.canvas.draw()
bbox = x.get_window_extent()

will return a Bbox object for your text in display units (the draw is necessary so that the text is rendered and actually has a display size). You can then use the transforms to convert it to which ever coordinate system you want. Ex

bbox_data = ax.transData.inverted().transform(bbox) 
like image 109
tacaswell Avatar answered Oct 06 '22 01:10

tacaswell