Skip to content

Commit

Permalink
plotters: Adjust to new renderer handling in Matplotlib 3.6
Browse files Browse the repository at this point in the history
The handling of the internal renderer objects changed in Matplotlib 3.6, and we
haven't adjusted to the new API. Move the existing logic into a helper function
so we can use it consistently, and try calling the new matplotlib API before
falling back to the old logic.

Fixes #291.

Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
  • Loading branch information
tohojo committed Jan 16, 2024
1 parent e8e8adf commit e4c5f65
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions flent/plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,14 +952,32 @@ def save_pdf(self, filename, data_filename, save_args):
finally:
pdf.close()

def _get_renderer(self):

# Matplotlib 3.6 reorganised the renderer caching logic and introduced
# the _get_renderer() method of the figure class, so try that first
try:
return self.figure._get_renderer()
except AttributeError:
pass

# Older versions of matplotlib cached the renderer in the figure itself,
# but it may not exist until after figure is drawn
renderer = getattr(self.figure, '_cachedRenderer', None)
if not renderer:
self.figure.canvas.draw()
renderer = getattr(self.figure, '_cachedRenderer', None)

return renderer

def build_tight_layout(self, artists):
args = None
if self.fallback_layout:
return {'bbox_extra_artists': artists, 'bbox_inches': 'tight'}
try:
self.figure.savefig(io.BytesIO())

renderer = self.figure._cachedRenderer
renderer = self._get_renderer()
right = x_max = self.figure.get_figwidth() * self.figure.dpi
top = y_max = self.figure.get_figheight() * self.figure.dpi
vsp = 0.02 * self.figure.dpi
Expand Down Expand Up @@ -1038,15 +1056,9 @@ def size_legends(self, event=None):
and not self.legend_placement \
and self.legends:

# Make sure we have a renderer to get size from
renderer = getattr(self.figure, '_cachedRenderer', None)
if not renderer:
self.figure.canvas.draw()
renderer = getattr(self.figure, '_cachedRenderer', None)

try:
legend_width = max(
[l.get_window_extent(renderer).width for l in self.legends])
[l.get_window_extent(self._get_renderer()).width for l in self.legends])
except Exception as e:
logger.debug("Error getting legend sizes: %s", e)
return
Expand Down

0 comments on commit e4c5f65

Please sign in to comment.