Skip to content

Backport PR #19949 on branch v3.4.x (FIX: subfigure indexing error) #20022

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
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
4 changes: 2 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down