Get bbox in data coordinates in matplotlib

0 votes

I have the bbox of a matplotlib.patches.Rectangle object (a bar from a bar graph) in display coordinates, like this:

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

But I don't want that in display coordinates but data coordinates. I'm pretty sure this requires a transform. Can anyone tell me the method for doing this?

Jun 18, 2019 in Python by ana1504.k
• 7,910 points
4,785 views

1 answer to this question.

0 votes

I'm not sure how you got the Bbox in display coordinates. Almost everything the user interacts with is in data coordinates. The following should fully explain the transforms as they apply 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       ]]))
answered Jun 18, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer

i am normalizing the data set iris in python and get the error ::TypeError: 'numpy.float64' object is not callable

TRY THIS   #Nomalisation for i in names:     print(i)   ...READ MORE

answered Aug 20, 2019 in Python by Noel Deepak Palle
5,891 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
15,267 views
+1 vote
1 answer

How to create plots using python matplotlib in IPython notebook?

I think you should try: I used %matplotlib inline in ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 58,020 points
1,566 views
+1 vote
1 answer

How to handle Real-Time Matplotlib Plotting

To draw a continuous set of random ...READ MORE

answered Sep 26, 2018 in Python by Priyaj
• 58,020 points
16,391 views
0 votes
1 answer

How to increase plt.title font size?

Try the following : import matplotlib.pyplot as plt plt.figtext(.5,.9,'Temperature', ...READ MORE

answered Feb 11, 2019 in Python by SDeb
• 13,300 points
1,162 views
0 votes
1 answer

Get the Current time in Python

To get the current date and time ...READ MORE

answered Oct 3, 2018 in Python by SDeb
• 13,300 points
650 views
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,300 points
943 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP