diff --git a/lib/matplotlib/_layoutgrid.py b/lib/matplotlib/_layoutgrid.py index 80a0ee2c86fb..8b7b140f600b 100644 --- a/lib/matplotlib/_layoutgrid.py +++ b/lib/matplotlib/_layoutgrid.py @@ -169,7 +169,8 @@ def hard_constraints(self): self.solver.addConstraint(c | 'required') def add_child(self, child, i=0, j=0): - self.children[i, j] = child + # np.ix_ returns the cross product of i and j indices + self.children[np.ix_(np.atleast_1d(i), np.atleast_1d(j))] = child def parent_constraints(self): # constraints that are due to the parent... diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index a8222a73d5ee..a717eace8348 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -560,3 +560,10 @@ def test_suplabels(): pos = ax.get_tightbbox(fig.canvas.get_renderer()) assert pos.y0 > pos0.y0 + 10.0 assert pos.x0 > pos0.x0 + 10.0 + + +def test_gridspec_addressing(): + fig = plt.figure() + gs = fig.add_gridspec(3, 3) + sp = fig.add_subplot(gs[0:, 1:]) + fig.draw_without_rendering() diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index bc2256ce3c54..cb8f63893aea 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -1073,6 +1073,7 @@ def test_subfigure_spanning(): fig.add_subfigure(gs[0, 0]), fig.add_subfigure(gs[0:2, 1]), fig.add_subfigure(gs[2, 1:3]), + fig.add_subfigure(gs[0:, 1:]) ] w = 640 @@ -1086,6 +1087,12 @@ def test_subfigure_spanning(): np.testing.assert_allclose(sub_figs[2].bbox.min, [w / 3, 0]) np.testing.assert_allclose(sub_figs[2].bbox.max, [w, h / 3]) + # check here that slicing actually works. Last sub_fig + # with open slices failed, but only on draw... + for i in range(4): + sub_figs[i].add_subplot() + fig.draw_without_rendering() + @mpl.style.context('mpl20') def test_subfigure_ticks():