Skip to content

FIX: use cached renderer on Legend.get_window_extent #11971

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
merged 1 commit into from
Sep 7, 2018
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
6 changes: 4 additions & 2 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,9 +976,11 @@ def get_title(self):
'Return the `.Text` instance for the legend title.'
return self._legend_title_box._text

def get_window_extent(self, *args, **kwargs):
def get_window_extent(self, renderer=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's technically a backward incompatible change (although, as I've mentioned in a previous PR doing something similar, I think this is a change that is worth making.)

'Return extent of the legend.'
return self._legend_box.get_window_extent(*args, **kwargs)
if renderer is None:
renderer = self.figure._cachedRenderer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we have lots of get_window_extent(renderer=...) in the code base. Is it all necessary or could a lot of it be safely replaced by this? I guess I can understand for objects that don't have a reference to their figure, but in general, I think most objects know how to get back to their containing figure...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to always fall back to the cached renderer. There are some use-cases (such as the mixed-mode vector/raster cases where we need to be able to specify which renderer we want to be using with out having to propgate that state globally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the mixed mode renderer is the only case where this could be a problem, right?
But in fact even for the mixed mode renderer we're not doing this correctly right now, because artist.draw(renderer) will store the instance of MixedModeRenderer as the _renderer attribute, so e.g. even if the artist was drawn rasterized, by the end of the draw the MixedModeRenderer will have reset itself to vectorized and later calls to get_window_extent() will use the cached MixedModeRenderer in the vectorized state.

return self._legend_box.get_window_extent(renderer=renderer)

def get_tightbbox(self, renderer):
"""
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,17 @@ def test_legend_proper_window_extent():
assert pytest.approx(x01*2, 0.1) == x02


def test_window_extent_cached_renderer():
fig, ax = plt.subplots(dpi=100)
ax.plot(range(10), label='Aardvark')
leg = ax.legend()
leg2 = fig.legend()
fig.canvas.draw()
# check that get_window_extent will use the cached renderer
leg.get_window_extent()
leg2.get_window_extent()


def test_legend_title_fontsize():
# test the title_fontsize kwarg
fig, ax = plt.subplots()
Expand Down