Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #26184 on branch v3.7.x (FIX: AnnotationBbox extents before draw) #26227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,11 +1399,15 @@ def get_window_extent(self, renderer=None):
# docstring inherited
if renderer is None:
renderer = self.figure._get_renderer()
self.update_positions(renderer)
return Bbox.union([child.get_window_extent(renderer)
for child in self.get_children()])

def get_tightbbox(self, renderer=None):
# docstring inherited
if renderer is None:
renderer = self.figure._get_renderer()
self.update_positions(renderer)
return Bbox.union([child.get_tightbbox(renderer)
for child in self.get_children()])

Expand Down
24 changes: 8 additions & 16 deletions lib/matplotlib/tests/test_offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ def test_anchoredtext_horizontal_alignment():
ax.add_artist(text2)


def test_annotationbbox_extents():
@pytest.mark.parametrize("extent_kind", ["window_extent", "tightbbox"])
def test_annotationbbox_extents(extent_kind):
plt.rcParams.update(plt.rcParamsDefault)
fig, ax = plt.subplots(figsize=(4, 3), dpi=100)

Expand All @@ -283,31 +284,22 @@ def test_annotationbbox_extents():
arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab6)

fig.canvas.draw()
renderer = fig.canvas.get_renderer()

# Test Annotation
bb1w = an1.get_window_extent(renderer)
bb1e = an1.get_tightbbox(renderer)
bb1 = getattr(an1, f"get_{extent_kind}")()

target1 = [332.9, 242.8, 467.0, 298.9]
assert_allclose(bb1w.extents, target1, atol=2)
assert_allclose(bb1e.extents, target1, atol=2)
assert_allclose(bb1.extents, target1, atol=2)

# Test AnnotationBbox
bb3w = ab3.get_window_extent(renderer)
bb3e = ab3.get_tightbbox(renderer)
bb3 = getattr(ab3, f"get_{extent_kind}")()

target3 = [-17.6, 129.0, 200.7, 167.9]
assert_allclose(bb3w.extents, target3, atol=2)
assert_allclose(bb3e.extents, target3, atol=2)
assert_allclose(bb3.extents, target3, atol=2)

bb6w = ab6.get_window_extent(renderer)
bb6e = ab6.get_tightbbox(renderer)
bb6 = getattr(ab6, f"get_{extent_kind}")()

target6 = [180.0, -32.0, 230.0, 92.9]
assert_allclose(bb6w.extents, target6, atol=2)
assert_allclose(bb6e.extents, target6, atol=2)
assert_allclose(bb6.extents, target6, atol=2)

# Test bbox_inches='tight'
buf = io.BytesIO()
Expand Down