Skip to content

bboxes coordinate system of plt.annotate objects depends on range of xycoords #8475

Closed

Description

@feilchenfeldt

Bug report

When I plot ax.annotate objects, and then want to get the coordinates of their bboxes using fig.canvas.draw() and annotation.get_window_extent(), then the coordinate system of the bboxes depends on whether the xy of the object is between 0 and 1 (including 0 and 1), even if xycords is always set to 'data'.

In particular, if xycords are between 0 and 1 then the bbox coordinates can be transformed into data coordinates using ax.transData.inverted().transform. This is not true for xycords not in [0,1].

Code for reproduction

from matplotlib import pyplot as plt
fig = plt.figure()
ax = plt.gca()

anns = []
xcoords = [0.5,1,1.5]
y = 0.5

for x in xcoords:
    ann = ax.annotate("Test",xy=(x, y), xycoords='data', verticalalignment='bottom')
    anns.append(ann)            

fig.canvas.draw()

for x, ann in zip(xcoords, anns):
    bbox = ann.get_window_extent()
    (xmin,ymin), (xmax,ymax) = ax.transData.inverted().transform(bbox)
    print bbox
    print x, '==', xmin
    print y,'==', ymin

Actual outcome

Bbox(x0=221.4, y0=147.6, x1=247.24375, y1=160.6)
0.5 == 0.5
0.5 == 0.5
Bbox(x0=388.8, y0=147.6, x1=414.64375, y1=160.6)
1 == 1.0
0.5 == 0.5
Bbox(x0=1.5, y0=0.5, x1=27.34375, y1=13.5)
1.5 == -0.156810035842
0.5 == -0.159050179211

Expected outcome

I would expect all Bbox objects to be in the same coordinate system (figure coordinates?), but they clearly are not.
I would expect a behaviour where the last two lines of the output above become

1.5 == 1.5
0.5 == 0.5

Matplotlib version

'1.5.3' from anaconda2
I am running this in a jupyter notebook with %matplotlib_inline

Activity

  1. ImportanceOfBeingErnest commented on Mar 4, 2020

    @ImportanceOfBeingErnest
    Member

    I'm not able to reproduce this. Running the code with matplotlib 3.1.0 produces the expected output.

    image

    Maybe this has solved itself throughout the versions since 1.5. If anyone still has this issue please ask to reopen and provide further details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions