8000 FIX: use cached renderer on Legend.get_window_extent · matplotlib/matplotlib@e1e4307 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e1e4307

Browse files
committed
FIX: use cached renderer on Legend.get_window_extent
Closes #11970
1 parent fb0035f commit e1e4307

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/matplotlib/legend.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,9 +976,11 @@ def get_title(self):
976976
'Return the `.Text` instance for the legend title.'
977977
return self._legend_title_box._text
978978

979-
def get_window_extent(self, *args, **kwargs):
979+
def get_window_extent(self, renderer=None):
980980
'Return extent of the legend.'
981-
return self._legend_box.get_window_extent(*args, **kwargs)
981+
if renderer is None:
982+
renderer = self.figure._cachedRenderer
983+
return self._legend_box.get_window_extent(renderer=renderer)
982984

983985
def get_tightbbox(self, renderer):
984986
"""

lib/matplotlib/tests/test_legend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,18 @@ def test_legend_proper_window_extent():
510510
assert pytest.approx(x01*2, 0.1) == x02
511511

512512

513+
def test_window_extent_cached_renderer():
514+
# test that legend returns the expected extent under various dpi...
515+
fig, ax = plt.subplots(dpi=100)
516+
ax.plot(range(10), label='Aardvark')
517+
leg = ax.legend()
518+
leg2 = fig.legend()
519+
fig.canvas.draw()
520+
521+
leg.get_window_extent()
522+
leg2.get_window_extent()
523+
524+
513525
def test_legend_title_fontsize():
514526
# test the title_fontsize kwarg
515527
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)
0