Skip to content

Fix SubFigure wspace/hspace None defaulting to subplot rc spacing - #32080

Closed
Fstarnb wants to merge 1 commit into
matplotlib:mainfrom
Fstarnb:fix/subfigures-none-spacing
Closed

Fstarnb wants to merge 1 commit into
matplotlib:mainfrom
Fstarnb:fix/subfigures-none-spacing

Conversation

@Fstarnb

@Fstarnb Fstarnb commented Jul 20, 2026 •

Copy link
Copy Markdown

PR summary

Without a layout engine, Figure.subfigures documents that unspecified wspace / hspace means zero. GridSpec still substitutes figure.subplot.{w,h}space rc defaults (typically 0.2) when given None, so:

  • subfigures(..., wspace=0.2, hspace=None) incorrectly added a vertical gap
  • subfigures(..., wspace=None, hspace=0.2) incorrectly added a horizontal gap

This is the regression reported in #32076 (related to the post-#25960 path).

Why this change: the docstring already promises “zero if not using a layout engine.” Passing only one spacing currently violates that for the unspecified axis. Explicit hspace=0 works as a workaround (as @rcomer noted on the issue), but the documented default should hold without requiring both arguments.

Implementation: when self.get_layout_engine() is None, coerce missing wspace/hspace to 0.0 before constructing the GridSpec. Layout-engine paths are unchanged (they still infer from rc / the engine).

Minimal example (bug on main):

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(6, 4), dpi=100)
sfs = fig.subfigures(2, 2, wspace=0.2, hspace=None)
for row in sfs:
    for sf in row:
        sf.add_subplot().plot([0, 1], [0, 1])
fig.canvas.draw()
print(sfs[0, 0].bbox.height)  # ~181.82 on main; 200.0 with this fix

Closes #32076.

AI Disclosure

I used generative AI assistance for:

  • Exploring the subfigures / GridSpec call path and drafting an initial patch sketch
  • Drafting parts of the regression test and PR wording

I personally reproduced the bug on main (Agg), chose the coerce-to-0.0 location to match the existing docstring, wrote/adjusted the regression test (test_subfigures_partial_spacing_none_is_zero), and verified red→green locally (height 181.82 → 200.0 for the issue reproducer; existing test_subfigures_wspace_hspace still passes). I am responsible for the change and can follow up on review.

Policy read: https://matplotlib.org/devdocs/devel/contribute.html#use-of-generative-ai

PR checklist

  • "closes SubFigure hspace=None incorrectly applies default vertical spacing when wspace is specified #32076" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example (bugfix; covered by unit test)
  • [N/A] New Features and API Changes are noted with a directive and release note (restores documented behavior)
  • [N/A] Documentation complies with general and docstring guidelines (docstring already correct; code now matches it)

Without a layout engine, Figure.subfigures documents that unspecified
wspace/hspace is zero. GridSpec still substitutes figure.subplot.* rc
defaults (0.2) for None, so setting only wspace applied a vertical gap
from the default hspace (and vice versa). Coerce missing spacings to 0.0
when no layout engine is active. Fixes matplotlib#32076.
@github-actions

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@rcomer

rcomer commented Jul 22, 2026

Copy link
Copy Markdown
Member

This PR shows indications of having been generated by AI. Please check it against our policy and, if it complies, update the PR summary to use our template.
https://matplotlib.org/devdocs/devel/contribute.html#use-of-generative-ai

@rcomer rcomer added the status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks label Jul 22, 2026
@github-actions

Copy link
Copy Markdown

⏰ This pull request might be automatically closed in two weeks from now.

Thank you for your contribution to Matplotlib and for the effort you have put into this PR. This pull request does not yet meet the quality and clarity standards needed for an effective review. Project maintainers have limited time for code reviews, and our goal is to prioritize well-prepared contributions to keep Matplotlib maintainable.

Matplotlib maintainers cannot provide one-to-one guidance on this PR. However, if you ask focused, well-researched questions, a community member may be willing to help. 💬

To increase the chance of a productive review:

As the author, you are responsible for driving this PR, which entails doing necessary background research as well as presenting its context and your thought process. If you are a new contributor, or do not know how to fulfill these requirements, we recommend that you familiarize yourself with Matplotlib's development conventions or engage with the community via our Discourse or one of our meetings before submitting code.

If you substantially improve this PR within two weeks, leave a comment and a team member may remove the status: autoclose candidate label and the PR stays open. Cosmetic changes or incomplete fixes will not be sufficient. Maintainers will assess improvements on their own schedule. Please do not ping (@) maintainers.

@Fstarnb

Fstarnb commented Jul 28, 2026

Copy link
Copy Markdown
Author

@rcomer Thanks for the flag — I’ve updated this PR to the current template and added an AI Disclosure section consistent with https://matplotlib.org/devdocs/devel/contribute.html#use-of-generative-ai.

Short version of the human side of the work:

  1. Reproduced SubFigure hspace=None incorrectly applies default vertical spacing when wspace is specified #32076 on main (Agg): with wspace=0.2, hspace=None, subfigure height was ~181.82 instead of 200 on a 400px-tall figure because GridSpec filled the missing hspace from figure.subplot.hspace (0.2).
  2. Confirmed the docstring already says unspecified spacing is zero when no layout engine is active.
  3. Fix is only on the no-layout-engine path: coerce None → 0.0 before GridSpec(...). Layout-engine behavior is untouched.
  4. Added test_subfigures_partial_spacing_none_is_zero for both-None, only-wspace, and only-hspace. Existing test_subfigures_wspace_hspace still passes.

On the issue you also noted that passing hspace=0 explicitly works, and that documenting “pass both” would be an easier doc-only fix. I’m happy either way maintainers prefer; this PR takes the “make code match the existing docstring” route rather than weakening the docs.

Note on duplicates: #32098 (opened two days after this one) implements essentially the same coerce-to-zero approach. Happy to close in favor of that PR, or keep this one — whichever is easier for review. I won’t open further matplotlib PRs while this is in flight.

@Fstarnb

Fstarnb commented Jul 28, 2026

Copy link
Copy Markdown
Author

Closing this PR. Thanks for the time on the review flag — I will not pursue this change further here.

@Fstarnb Fstarnb closed this Jul 28, 2026
@Fstarnb
Fstarnb deleted the fix/subfigures-none-spacing branch July 28, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

first-contribution status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks topic: figures and subfigures

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SubFigure hspace=None incorrectly applies default vertical spacing when wspace is specified

2 participants