Get bbox in data coordinates in matplotlib

I have a bbox object (a line from a histogram) in the display coordinates, for example:

 Bbox(array([[ 0., 0.],[ 1., 1.]]) 

But I would like this to not display the coordinates, but the coordinates of the data. I am sure that this requires conversion. What is the method for this?

+4
source share
1 answer

I'm not sure how you got the Bbox in the displayed coordinates. Almost everything the user interacts with is in the coordinates of the data (they change the coordinates of the axis or data, rather than displaying pixels). The following should fully explain the transformations applicable to Bboxes:

 from matplotlib import pyplot as plt bars = plt.bar([1,2,3],[3,4,5]) ax = plt.gca() fig = plt.gcf() b = bars[0].get_bbox() # bbox instance print b # box in data coords #Bbox(array([[ 1. , 0. ], # [ 1.8, 3. ]])) b2 = b.transformed(ax.transData) print b2 # box in display coords #Bbox(array([[ 80. , 48. ], # [ 212.26666667, 278.4 ]])) print b2.transformed(ax.transData.inverted()) # box back in data coords #Bbox(array([[ 1. , 0. ], # [ 1.8, 3. ]])) print b2.transformed(ax.transAxes.inverted()) # box in axes coordinates #Bbox(array([[ 0. , 0. ], # [ 0.26666667, 0.6 ]])) 
+9
source

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


All Articles