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

Skip to content

Commit e2fa1ae

Browse files
committed
FIX: wspace and hspace in subfigures without layout engine
1 parent 7d7f6da commit e2fa1ae

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/matplotlib/figure.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,8 +1564,10 @@ def subfigures(self, nrows=1, ncols=1, squeeze=True,
15641564
wspace, hspace : float, default: None
15651565
The amount of width/height reserved for space between subfigures,
15661566
expressed as a fraction of the average subfigure width/height.
1567-
If not given, the values will be inferred from a figure or
1568-
rcParams when necessary.
1567+
If not given, the values will be inferred from rcParams if using
1568+
constrained layout (see
1569+
:class:`~matplotlib.layout_engine.ConstrainedLayoutEngine`), or
1570+
zero if not using a layout engine.
15691571
15701572
width_ratios : array-like of length *ncols*, optional
15711573
Defines the relative widths of the columns. Each column gets a
@@ -1580,13 +1582,24 @@ def subfigures(self, nrows=1, ncols=1, squeeze=True,
15801582
gs = GridSpec(nrows=nrows, ncols=ncols, figure=self,
15811583
wspace=wspace, hspace=hspace,
15821584
width_ratios=width_ratios,
1583-
height_ratios=height_ratios)
1585+
height_ratios=height_ratios,
1586+
left=0, right=1, bottom=0, top=1)
15841587

15851588
sfarr = np.empty((nrows, ncols), dtype=object)
15861589
for i in range(ncols):
15871590
for j in range(nrows):
15881591
sfarr[j, i] = self.add_subfigure(gs[j, i], **kwargs)
15891592

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

lib/matplotlib/tests/test_figure.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,25 @@ def test_subfigure_pdf():
14491449
fig.savefig(buffer, format='pdf')
14501450

14511451

1452+
def test_subfigures_wspace_hspace():
1453+
sub_figs = plt.figure().subfigures(2, 2, hspace=0.5, wspace=0.5)
1454+
1455+
w = 640
1456+
h = 480
1457+
1458+
np.testing.assert_allclose(sub_figs[0, 0].bbox.min, [0., h * 0.6])
1459+
np.testing.assert_allclose(sub_figs[0, 0].bbox.max, [w * 0.4, h])
1460+
1461+
np.testing.assert_allclose(sub_figs[0, 1].bbox.min, [w * 0.6, h * 0.6])
1462+
np.testing.assert_allclose(sub_figs[0, 1].bbox.max, [w, h])
1463+
1464+
np.testing.assert_allclose(sub_figs[1, 0].bbox.min, [0, 0])
1465+
np.testing.assert_allclose(sub_figs[1, 0].bbox.max, [w * 0.4, h * 0.4])
1466+
1467+
np.testing.assert_allclose(sub_figs[1, 1].bbox.min, [w * 0.6, 0])
1468+
np.testing.assert_allclose(sub_figs[1, 1].bbox.max, [w, h * 0.4])
1469+
1470+
14521471
def test_add_subplot_kwargs():
14531472
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
14541473
fig = plt.figure()

0 commit comments

Comments
 (0)