ENH: Support partial figsize with None (#31400)#31413
Open
Vikash-Kumar-23 wants to merge 2 commits intomatplotlib:mainfrom
Open
ENH: Support partial figsize with None (#31400)#31413Vikash-Kumar-23 wants to merge 2 commits intomatplotlib:mainfrom
Vikash-Kumar-23 wants to merge 2 commits intomatplotlib:mainfrom
Conversation
Member
|
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. |
Contributor
Author
|
Thanks @timhoffm for the clarification and for addressing the open questions on #31400.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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