forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
bugSomething isn't workingSomething isn't working
Description
User Request
[Bug]: wspace and hspace in subfigures not working
Bug summary:
wspaceandhspaceinFigure.subfiguresdo nothing.
Code for reproduction:
import matplotlib.pyplot as plt
figs = plt.figure().subfigures(2, 2, wspace=0, hspace=0)
for fig in figs.flat:
fig.subplots().plot([1, 2])
plt.show()Actual outcome:
- Same figure independently of the values of hspace and wspace.
Expected outcome:
- Spacing should be reflected, consistent with Figure.subfigures behavior (see upstream reference in figure.py around lines 1550-1554).
Environment (from reporter):
- OS/X, Matplotlib 3.7.1, Backend MacOSX, Python 3.10.9, install via conda.
Research Specification (from Emerson Gray)
Summary:
- In non-constrained layout, SubFigure placement ignores GridSpec’s wspace/hspace. The fix is to compute SubFigure bbox in parent coordinates applying provided
wspaceandhspacefromFigure.subfigures.
Key code paths:
lib/matplotlib/figure.py:Figure.subfigures(...)creates a GridSpec and SubFigures.SubFigure._redo_transform_rel_fig(bbox=None)computes subfigure region but currently ignores spacing unless constrained layout passes an explicit bbox.
lib/matplotlib/gridspec.py:GridSpec.get_grid_positions(...)shows the reference logic for spacing.lib/matplotlib/_constrained_layout.py: constrained layout path already handles spacing by calling_redo_transform_rel_figwith a bbox.
Algorithm (non-constrained):
- When
_redo_transform_rel_figis called withbbox=None, compute the subfigure’s rectangle in the parent coordinate system [0,1] usingnrows/ncols, width/height ratios, andwspace/hspacefrom the GridSpec created byFigure.subfigures. - Treat
wspace/hspaceas 0.0 when None (backward-compatible default; previously there were no gaps). - Do not involve
figure.subplotparsmargins; subfigures fill the parent area. - For a SubplotSpec span (e.g.,
gs[r0:r1, c0:c1]), use min left/max right and min bottom/max top over spanned cells including internal separators.
Edge cases:
- (wspace,hspace) = (0,0): subfigures touch.
- nrows=1 or ncols=1: spacing has no effect (full size).
- Nested subfigures: compute inside the parent’s [0,1] space.
- Mixed subplots and subfigures: leave constrained behavior unchanged; non-constrained defaults remain zero spacing if None.
Testing plan:
- Add geometry-based tests checking that
wspace/hspaceproduce measurable gaps without constrained layout. - Test that default None yields no gaps.
- Test nested subfigures behavior.
Implementation changes:
- Modify
SubFigure._redo_transform_rel_fig(self, bbox=None)inlib/matplotlib/figure.pyto computebbox_relativeusing spacing per the algorithm above whenbboxis None. Keep thebbox is not Nonebranch unchanged.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working