Skip to content

Simplify _redo_transform_rel_fig. #20023

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
Apr 20, 2021
Merged
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
37 changes: 9 additions & 28 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down