Skip to content

Added logic to apply wspace and hspace in subfigures without constrained layout #25646

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

Closed
Closed
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
25 changes: 25 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2248,12 +2248,37 @@ def _redo_transform_rel_fig(self, bbox=None):
return
# need to figure out *where* this subplotspec is.
gs = self._subplotspec.get_gridspec()
subplot_params = gs.get_subplot_params(self)
wr = np.asarray(gs.get_width_ratios())
hr = np.asarray(gs.get_height_ratios())
dx = wr[self._subplotspec.colspan].sum() / wr.sum()
dy = hr[self._subplotspec.rowspan].sum() / hr.sum()
x0 = wr[:self._subplotspec.colspan.start].sum() / wr.sum()
y0 = 1 - hr[:self._subplotspec.rowspan.stop].sum() / hr.sum()
wspace = subplot_params.wspace
hspace = subplot_params.hspace
if not self._subplotspec.is_first_row():
# add space to the top
avg_width = 1 / len(wr)
padding = (hspace * avg_width) * dy
dy -= padding
if not self._subplotspec.is_first_col():
# add space to the left
avg_width = 1 / len(wr)
padding = (wspace * avg_width) * dx
dx -= padding
x0 += padding
if not self._subplotspec.is_last_row():
# add space to the bottom
avg_width = 1 / len(wr)
padding = (hspace * avg_width) * dy
dy -= padding
y0 += padding
if not self._subplotspec.is_last_col():
# add space to the right
avg_width = 1 / len(wr)
padding = (wspace * avg_width) * dx
dx -= padding
if self.bbox_relative is None:
self.bbox_relative = Bbox.from_bounds(x0, y0, dx, dy)
else:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,15 @@ def test_tightbbox_box_aspect():
ax2.set_box_aspect((2, 1, 1))


@image_comparison(['test_wspace_hspace_subfigure.png'],
remove_text=True)
def test_wspace_hspace_subfigure():
figs = plt.figure().subfigures(2, 2, hspace=0.2, wspace=0.2)
for fig, color in zip(figs.flat, 'cmyw'):
fig.set_facecolor(color)
fig.subplots().plot([1, 2])


@check_figures_equal(extensions=["svg", "pdf", "eps", "png"])
def test_animated_with_canvas_change(fig_test, fig_ref):
ax_ref = fig_ref.subplots()
Expand Down