diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index b301149961f7..a83a25825ba5 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1648,8 +1648,9 @@ 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 @@ -1657,9 +1658,11 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None): _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): diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 5ab1f35a1a2c..ea184bd03af8 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -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',