Skip to content

FIX: bbox subfigures #21845

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
Dec 3, 2021
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
9 changes: 6 additions & 3 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,18 +1648,21 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None):
if (np.isfinite(b.width) and np.isfinite(b.height)
and (b.width != 0 or b.height != 0))]

isfigure = hasattr(self, 'bbox_inches')
if len(bb) == 0:
if hasattr(self, 'bbox_inches'):
if isfigure:
return self.bbox_inches
else:
# subfigures do not have bbox_inches, but do have a bbox
bb = [self.bbox]

_bbox = Bbox.union(bb)

bbox_inches = TransformedBbox(_bbox, Affine2D().scale(1 / self.dpi))
if isfigure:
# transform from pixels to inches...
_bbox = TransformedBbox(_bbox, self.dpi_scale_trans.inverted())

return bbox_inches
return _bbox

@staticmethod
def _normalize_grid_string(layout):
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,8 @@ def test_subfigure_tightbbox():
sub = fig.subfigures(1, 2)

np.testing.assert_allclose(
fig.get_tightbbox(fig.canvas.get_renderer()).width, 0.1)
fig.get_tightbbox(fig.canvas.get_renderer()).width,
8.0)


@image_comparison(['test_subfigure_ss.png'], style='mpl20',
Expand Down