Skip to content

Commit 704a37c

Browse files
committed
FIX: subfigure indexing error
1 parent c278744 commit 704a37c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/matplotlib/figure.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1992,11 +1992,11 @@ def _redo_transform_rel_fig(self, bbox=None):
19921992

19931993
x0 = 0
19941994
if not self._subplotspec.is_first_col():
1995-
x0 += np.sum(wr[self._subplotspec.colspan.start - 1]) / np.sum(wr)
1995+
x0 += np.sum(wr[:self._subplotspec.colspan.start]) / np.sum(wr)
19961996

19971997
y0 = 0
19981998
if not self._subplotspec.is_last_row():
1999-
y0 += 1 - (np.sum(hr[self._subplotspec.rowspan.stop - 1]) /
1999+
y0 += 1 - (np.sum(hr[:(self._subplotspec.rowspan.stop)]) /
20002000
np.sum(hr))
20012001

20022002
if self.bbox_relative is None:

lib/matplotlib/tests/test_figure.py

+33
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,39 @@ def test_subfigure_double():
967967
axsRight = subfigs[1].subplots(2, 2)
968968

969969

970+
def test_subfigure_spanning():
971+
# test that subfigures get laid out properly...
972+
fig = plt.figure(constrained_layout=True)
973+
gs = fig.add_gridspec(3, 3)
974+
sub_figs = []
975+
sub_figs += [fig.add_subfigure(gs[0, 0])]
976+
sub_figs += [fig.add_subfigure(gs[0:2, 1])]
977+
sub_figs += [fig.add_subfigure(gs[2, 1:3])]
978+
979+
w = 640
980+
h = 480
981+
minp = sub_figs[0].bbox.min
982+
exp = np.array([0., h*2/3])
983+
np.testing.assert_allclose(minp, exp)
984+
maxp = sub_figs[0].bbox.max
985+
exp = np.array([w / 3, h])
986+
np.testing.assert_allclose(maxp, exp)
987+
988+
minp = sub_figs[1].bbox.min
989+
exp = np.array([w / 3, h * 1 / 3])
990+
np.testing.assert_allclose(minp, exp)
991+
maxp = sub_figs[1].bbox.max
992+
exp = np.array([w * 2 / 3, h])
993+
np.testing.assert_allclose(maxp, exp)
994+
995+
minp = sub_figs[2].bbox.min
996+
exp = np.array([w / 3, 0])
997+
np.testing.assert_allclose(minp, exp)
998+
maxp = sub_figs[2].bbox.max
999+
exp = np.array([w, h * 1 / 3])
1000+
np.testing.assert_allclose(maxp, exp)
1001+
1002+
9701003
def test_add_subplot_kwargs():
9711004
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
9721005
fig = plt.figure()

0 commit comments

Comments
 (0)