ENH: Support partial figsize with None (#31400)#31413
ENH: Support partial figsize with None (#31400)#31413QuLogic merged 12 commits intomatplotlib:mainfrom
Conversation
|
Thanks for the PR. There's been open questions on the original issue. I've answered them now, in particular #31400 (comment). Please adapt accordingly. Please note that realizing the open questions and clarifying them (or at least explaining why you have solved them in a specific way) is part of your design and validation responsibility. Addressing them up-front saves unncessary implementation and review effort. |
|
Thanks @timhoffm for the clarification and for addressing the open questions on #31400.
|
Co-authored-by: Tim Hoffmann <[email protected]>
Co-authored-by: Tim Hoffmann <[email protected]>
Co-authored-by: Tim Hoffmann <[email protected]>
Co-authored-by: Tim Hoffmann <[email protected]>
timhoffm
left a comment
There was a problem hiding this comment.
Please add documentation to the figsize parameter in the relevant docstring.
Also, please add a what's new note: https://matplotlib.org/devdocs/devel/api_changes.html#what-s-new-notes
|
Hi @timhoffm, Thanks for the review! I’ve also added a “What’s New” entry in doc/release/next_whats_new/partial_figsize_none.rst. |
Co-authored-by: Tim Hoffmann <[email protected]>
Co-authored-by: Tim Hoffmann <[email protected]>
Co-authored-by: Tim Hoffmann <[email protected]>
Closes #31400
PR summary
This PR updates figsize handling based on the discussion in #31400.
Partial None is supported only when creating a figure:
plt.figure(figsize=(None, h)) uses the default width from rcParams["figure.figsize"]
plt.figure(figsize=(w, None)) uses the default height from rcParams["figure.figsize"]
If both values are None (figsize=(None, None)), a clear ValueError is raised.
Figure.set_size_inches now requires explicit width and height and rejects None to avoid ambiguous behavior.
changes
Updated figure creation logic to resolve a single missing figsize dimension from rcParams before numeric validation.
Updated Figure.set_size_inches to reject None for either width or height (no fallback/guessing).
Ensured None never reaches finite/positivity checks.
updated tests for:
valid partial-None figure creation
invalid figsize=(None, None)
invalid Figure().set_size_inches(None, 3) and Figure().set_size_inches(6, None)
Examples
plt.figure(figsize=(None, 4)) # Uses default width, height = 4

plt.figure(figsize=(6, None)) # Uses default height, width = 6

Testing
Ran
python -m pytest lib/matplotlib/tests/test_figure.py
All tests passed ✅
AI Disclosure
I used AI assistance to help draft parts of the implementation and tests, as well as to refine the PR description. All changes were reviewed and validated manually.
PR checklist