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

Skip to content

Commit 4d199b0

Browse files
authored
Merge pull request #26227 from meeseeksmachine/auto-backport-of-pr-26184-on-v3.7.x
Backport PR #26184 on branch v3.7.x (FIX: AnnotationBbox extents before draw)
2 parents 9940416 + 303ea56 commit 4d199b0

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,11 +1399,15 @@ def get_window_extent(self, renderer=None):
13991399
# docstring inherited
14001400
if renderer is None:
14011401
renderer = self.figure._get_renderer()
1402+
self.update_positions(renderer)
14021403
return Bbox.union([child.get_window_extent(renderer)
14031404
for child in self.get_children()])
14041405

14051406
def get_tightbbox(self, renderer=None):
14061407
# docstring inherited
1408+
if renderer is None:
1409+
renderer = self.figure._get_renderer()
1410+
self.update_positions(renderer)
14071411
return Bbox.union([child.get_tightbbox(renderer)
14081412
for child in self.get_children()])
14091413

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ def test_anchoredtext_horizontal_alignment():
256256
ax.add_artist(text2)
257257

258258

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

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

286-
fig.canvas.draw()
287-
renderer = fig.canvas.get_renderer()
288-
289287
# Test Annotation
290-
bb1w = an1.get_window_extent(renderer)
291-
bb1e = an1.get_tightbbox(renderer)
288+
bb1 = getattr(an1, f"get_{extent_kind}")()
292289

293290
target1 = [332.9, 242.8, 467.0, 298.9]
294-
assert_allclose(bb1w.extents, target1, atol=2)
295-
assert_allclose(bb1e.extents, target1, atol=2)
291+
assert_allclose(bb1.extents, target1, atol=2)
296292

297293
# Test AnnotationBbox
298-
bb3w = ab3.get_window_extent(renderer)
299-
bb3e = ab3.get_tightbbox(renderer)
294+
bb3 = getattr(ab3, f"get_{extent_kind}")()
300295

301296
target3 = [-17.6, 129.0, 200.7, 167.9]
302-
assert_allclose(bb3w.extents, target3, atol=2)
303-
assert_allclose(bb3e.extents, target3, atol=2)
297+
assert_allclose(bb3.extents, target3, atol=2)
304298

305-
bb6w = ab6.get_window_extent(renderer)
306-
bb6e = ab6.get_tightbbox(renderer)
299+
bb6 = getattr(ab6, f"get_{extent_kind}")()
307300

308301
target6 = [180.0, -32.0, 230.0, 92.9]
309-
assert_allclose(bb6w.extents, target6, atol=2)
310-
assert_allclose(bb6e.extents, target6, atol=2)
302+
assert_allclose(bb6.extents, target6, atol=2)
311303

312304
# Test bbox_inches='tight'
313305
buf = io.BytesIO()

0 commit comments

Comments
 (0)