Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab837f6

Browse files
authored
Merge pull request #26260 from meeseeksmachine/auto-backport-of-pr-25960-on-v3.7.x
Backport PR #25960 on branch v3.7.x (FIX: wspace and hspace in subfigures without layout engine)
2 parents c5566a6 + ed29435 commit ab837f6

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/matplotlib/figure.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,8 +1570,9 @@ def subfigures(self, nrows=1, ncols=1, squeeze=True,
15701570
wspace, hspace : float, default: None
15711571
The amount of width/height reserved for space between subfigures,
15721572
expressed as a fraction of the average subfigure width/height.
1573-
If not given, the values will be inferred from a figure or
1574-
rcParams when necessary.
1573+
If not given, the values will be inferred from rcParams if using
1574+
constrained layout (see `~.ConstrainedLayoutEngine`), or zero if
1575+
not using a layout engine.
15751576
15761577
width_ratios : array-like of length *ncols*, optional
15771578
Defines the relative widths of the columns. Each column gets a
@@ -1586,13 +1587,24 @@ def subfigures(self, nrows=1, ncols=1, squeeze=True,
15861587
gs = GridSpec(nrows=nrows, ncols=ncols, figure=self,
15871588
wspace=wspace, hspace=hspace,
15881589
width_ratios=width_ratios,
1589-
height_ratios=height_ratios)
1590+
height_ratios=height_ratios,
1591+
left=0, right=1, bottom=0, top=1)
15901592

15911593
sfarr = np.empty((nrows, ncols), dtype=object)
15921594
for i in range(ncols):
15931595
for j in range(nrows):
15941596
sfarr[j, i] = self.add_subfigure(gs[j, i], **kwargs)
15951597

1598+
if self.get_layout_engine() is None and (wspace is not None or
1599+
hspace is not None):
1600+
# Gridspec wspace and hspace is ignored on subfigure instantiation,
1601+
# and no space is left. So need to account for it here if required.
1602+
bottoms, tops, lefts, rights = gs.get_grid_positions(self)
1603+
for sfrow, bottom, top in zip(sfarr, bottoms, tops):
1604+
for sf, left, right in zip(sfrow, lefts, rights):
1605+
bbox = Bbox.from_extents(left, bottom, right, top)
1606+
sf._redo_transform_rel_fig(bbox=bbox)
1607+
15961608
if squeeze:
15971609
# Discarding unneeded dimensions that equal 1. If we only have one
15981610
# subfigure, just return it instead of a 1-element array.

lib/matplotlib/tests/test_figure.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,31 @@ def test_subfigure_pdf():
13871387
fig.savefig(buffer, format='pdf')
13881388

13891389

1390+
def test_subfigures_wspace_hspace():
1391+
sub_figs = plt.figure().subfigures(2, 3, hspace=0.5, wspace=1/6.)
1392+
1393+
w = 640
1394+
h = 480
1395+
1396+
np.testing.assert_allclose(sub_figs[0, 0].bbox.min, [0., h * 0.6])
1397+
np.testing.assert_allclose(sub_figs[0, 0].bbox.max, [w * 0.3, h])
1398+
1399+
np.testing.assert_allclose(sub_figs[0, 1].bbox.min, [w * 0.35, h * 0.6])
1400+
np.testing.assert_allclose(sub_figs[0, 1].bbox.max, [w * 0.65, h])
1401+
1402+
np.testing.assert_allclose(sub_figs[0, 2].bbox.min, [w * 0.7, h * 0.6])
1403+
np.testing.assert_allclose(sub_figs[0, 2].bbox.max, [w, h])
1404+
1405+
np.testing.assert_allclose(sub_figs[1, 0].bbox.min, [0, 0])
1406+
np.testing.assert_allclose(sub_figs[1, 0].bbox.max, [w * 0.3, h * 0.4])
1407+
1408+
np.testing.assert_allclose(sub_figs[1, 1].bbox.min, [w * 0.35, 0])
1409+
np.testing.assert_allclose(sub_figs[1, 1].bbox.max, [w * 0.65, h * 0.4])
1410+
1411+
np.testing.assert_allclose(sub_figs[1, 2].bbox.min, [w * 0.7, 0])
1412+
np.testing.assert_allclose(sub_figs[1, 2].bbox.max, [w, h * 0.4])
1413+
1414+
13901415
def test_add_subplot_kwargs():
13911416
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
13921417
fig = plt.figure()

0 commit comments

Comments
 (0)