From 87bab66cb54c456014407a779188bc844f5d1b84 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 20 Apr 2021 13:55:42 +0200 Subject: [PATCH] Simplify _redo_transform_rel_fig. - width_ratios and height_ratios are never None (None gets normalized to [1, 1, ...] in the setters). - We don't need to separately handle the `is_first_row()/is_last_col()` cases (the sums will evaluate to 0/1 as needed). - Naming the width/height dx/dy makes everything line up neatly. --- lib/matplotlib/figure.py | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index d29a9d964cd1..a0c7c4e44fe7 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2011,42 +2011,23 @@ def _redo_transform_rel_fig(self, bbox=None): If not None, then the bbox is used for relative bounding box. Otherwise it is calculated from the subplotspec. """ - if bbox is not None: self.bbox_relative.p0 = bbox.p0 self.bbox_relative.p1 = bbox.p1 return - - gs = self._subplotspec.get_gridspec() # need to figure out *where* this subplotspec is. - wr = gs.get_width_ratios() - hr = gs.get_height_ratios() - nrows, ncols = gs.get_geometry() - if wr is None: - wr = np.ones(ncols) - else: - wr = np.array(wr) - if hr is None: - hr = np.ones(nrows) - else: - hr = np.array(hr) - widthf = np.sum(wr[self._subplotspec.colspan]) / np.sum(wr) - heightf = np.sum(hr[self._subplotspec.rowspan]) / np.sum(hr) - - x0 = 0 - if not self._subplotspec.is_first_col(): - x0 += np.sum(wr[:self._subplotspec.colspan.start]) / np.sum(wr) - - y0 = 0 - if not self._subplotspec.is_last_row(): - y0 += 1 - (np.sum(hr[:self._subplotspec.rowspan.stop]) / - np.sum(hr)) - + gs = self._subplotspec.get_gridspec() + wr = np.asarray(gs.get_width_ratios()) + hr = np.asarray(gs.get_height_ratios()) + dx = wr[self._subplotspec.colspan].sum() / wr.sum() + dy = hr[self._subplotspec.rowspan].sum() / hr.sum() + x0 = wr[:self._subplotspec.colspan.start].sum() / wr.sum() + y0 = 1 - hr[:self._subplotspec.rowspan.stop].sum() / hr.sum() if self.bbox_relative is None: - self.bbox_relative = Bbox.from_bounds(x0, y0, widthf, heightf) + self.bbox_relative = Bbox.from_bounds(x0, y0, dx, dy) else: self.bbox_relative.p0 = (x0, y0) - self.bbox_relative.p1 = (x0 + widthf, y0 + heightf) + self.bbox_relative.p1 = (x0 + dx, y0 + dy) def get_constrained_layout(self): """