The coordinates of the annotation window in matplotlib

How can I get the coordinates of the window displayed in the following graph?

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 check the properties of this object, but I could not find anything suitable for this purpose. There is a get_bbox_patch() property, which may be on the right path, however I get the results in a different coordinate system (or related to another property)

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

Thanks a lot!

+6
source share
2 answers
 ax.figure.canvas.draw() bbox = x.get_window_extent() 

will return a Bbox object for your text in display units ( draw is required for the text to be rendered and actually have a display size). You can then use the transforms to convert them to the coordinate system you need. Ex

 bbox_data = ax.transData.inverted().transform(bbox) 
+5
source

Your question also raises the question:

  • When you write How can I get the coordinates of the box displayed in the following plot? What coordinate system do you mean?

By default, annotate is executed using xytext = None, defaults to xy, and if textcoords = None, defaults to xycoords .

Since you did not specify a coordinate system. Your annotation is on the system by default. You can specify the coordinates of the data, which are good enough for some purposes:

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

To find the coordinate system, you can:

 In [39]: x.xycoords Out[39]: 'data' 

and get the coordinates:

 In [40]: x.xytext Out[40]: (0.0, 0.7) In [41]: x.xy Out[41]: (0.5, 0) 

PS is not directly connected, but exiting IPython , if you are still not using it, it can improve Python development efficiency and use matplotlib. Give it a try.

+1
source

Source: https://habr.com/ru/post/950032/


All Articles