diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 32c1d8d886ca..2af6e3db1e1c 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2037,11 +2037,11 @@ def _redo_transform_rel_fig(self, bbox=None): x0 = 0 if not self._subplotspec.is_first_col(): - x0 += np.sum(wr[self._subplotspec.colspan.start - 1]) / np.sum(wr) + 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 - 1]) / + y0 += 1 - (np.sum(hr[:self._subplotspec.rowspan.stop]) / np.sum(hr)) if self.bbox_relative is None: diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 62baf22159b5..70327354e778 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -998,6 +998,28 @@ def test_subfigure_double(): axsRight = subfigs[1].subplots(2, 2) +def test_subfigure_spanning(): + # test that subfigures get laid out properly... + fig = plt.figure(constrained_layout=True) + gs = fig.add_gridspec(3, 3) + sub_figs = [ + fig.add_subfigure(gs[0, 0]), + fig.add_subfigure(gs[0:2, 1]), + fig.add_subfigure(gs[2, 1:3]), + ] + + w = 640 + h = 480 + np.testing.assert_allclose(sub_figs[0].bbox.min, [0., h * 2/3]) + np.testing.assert_allclose(sub_figs[0].bbox.max, [w / 3, h]) + + np.testing.assert_allclose(sub_figs[1].bbox.min, [w / 3, h / 3]) + np.testing.assert_allclose(sub_figs[1].bbox.max, [w * 2/3, h]) + + np.testing.assert_allclose(sub_figs[2].bbox.min, [w / 3, 0]) + np.testing.assert_allclose(sub_figs[2].bbox.max, [w, h / 3]) + + def test_add_subplot_kwargs(): # fig.add_subplot() always creates new axes, even if axes kwargs differ. fig = plt.figure()