Skip to content

AnnotationBbox returns weird window_extent? #19530

Closed
@znstrider

Description

@znstrider

Bug report

Bug summary

A TextArea that is placed onto the figure with an AnnotationBbox at the same location as an ax.text returns a different window extent and I can't figure out why that is / how to get the window extent in the same coordinates.

Same for AnnotationBbox with xycoords=fig.transFigure and fig.text.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.offsetbox import AnnotationBbox, TextArea

fig, ax = plt.subplots(1, 2, figsize=(6, 4))

figtext = fig.text(s='text', x=0.5, y=0.5, ha='center', va='center', color='C1')

textarea = TextArea(s='text', textprops={'color': 'None'})
annobox = AnnotationBbox(textarea, xy=(0.5, 0.5),
                         bboxprops={'facecolor': 'lightgrey', 'alpha': 0.5, 'edgecolor': 'none'})
ax[0].add_artist(annobox)

axtext = ax[0].text(s='text', x=0.5, y=0.5, ha='center', va='center', color='C1', zorder=9)

textarea2 = TextArea(s='text', textprops={'color': 'None'})
annobox2 = AnnotationBbox(textarea2, xy=(0.5, 0.5), xycoords=fig.transFigure,
                         bboxprops={'facecolor': 'lightgrey', 'alpha': 0.5, 'edgecolor': 'none'})
ax[0].add_artist(annobox2)

plt.show()

renderer = fig.canvas.get_renderer()
print(axtext.get_window_extent(renderer),'\n',
      annobox.get_window_extent(renderer))

print(figtext.get_window_extent(renderer),'\n',
      annobox2.get_window_extent(renderer))

Actual outcome

Bbox(x0=120.27840909090907, y0=139.72, x1=139.90340909090907, y1=149.72) 
 Bbox(x0=184.5193181818182, y0=247.34000000000003, x1=383.9625, y1=282.34000000000003)

Bbox(x0=206.1875, y0=139.0, x1=225.8125, y1=149.0) 
 Bbox(x0=356.3375, y0=245.89999999999998, x1=411.9625, y1=280.9)

AnnotationBBox_example

Expected outcome

I would expect to get the same window_extents returned both times as they show above one another on the plot.

Matplotlib version

  • Operating system: MacOS
  • Matplotlib version 3.3.2
  • Matplotlib backend module://ipykernel.pylab.backend_inline
  • Python version:
  • Jupyter version (if applicable):
  • Other libraries:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions