Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Support partial figsize with None (#31400)#31413

Merged
QuLogic merged 12 commits intomatplotlib:mainfrom
Vikash-Kumar-23:figsize-partial-support
Mar 31, 2026
Merged

ENH: Support partial figsize with None (#31400)#31413
QuLogic merged 12 commits intomatplotlib:mainfrom
Vikash-Kumar-23:figsize-partial-support

Conversation

@Vikash-Kumar-23
Copy link
Copy Markdown
Contributor

@Vikash-Kumar-23 Vikash-Kumar-23 commented Mar 28, 2026

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
image

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

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

@timhoffm
Copy link
Copy Markdown
Member

timhoffm commented Mar 28, 2026

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.

@Vikash-Kumar-23
Copy link
Copy Markdown
Contributor Author

Thanks @timhoffm for the clarification and for addressing the open questions on #31400.
I’ve updated the PR based on that discussion.

  • Figure.set_size_inches now raises an error if either dimension is None (no fallback to rcParams and no implicit assumptions).
  • Using None is still supported during figure creation with figsize=(None, h) or (w, None), where the missing value is filled from rcParams["figure.figsize"].
  • figsize=(None, None) now correctly raises an error.

Comment thread lib/matplotlib/figure.py Outdated
Comment thread lib/matplotlib/figure.py Outdated
Comment thread lib/matplotlib/tests/test_figure.py Outdated
Comment thread lib/matplotlib/tests/test_figure.py Outdated
Comment thread lib/matplotlib/tests/test_figure.py Outdated
Copy link
Copy Markdown
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Vikash-Kumar-23
Copy link
Copy Markdown
Contributor Author

Hi @timhoffm,

Thanks for the review!
I've now added documentation for the figsize parameter in both lib/matplotlib/pyplot.py and lib/matplotlib/figure.py. It clearly explains that passing None for one dimension is allowed when creating a figure (and gets filled from rcParams["figure.figsize"]), while (None, None) still raises a ValueError.

I’ve also added a “What’s New” entry in doc/release/next_whats_new/partial_figsize_none.rst.

Copy link
Copy Markdown
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nits.

Comment thread doc/release/next_whats_new/partial_figsize_none.rst Outdated
Comment thread lib/matplotlib/figure.py Outdated
Comment thread lib/matplotlib/pyplot.py Outdated
@QuLogic QuLogic merged commit ee63e75 into matplotlib:main Mar 31, 2026
48 of 53 checks passed
@QuLogic QuLogic added this to the v3.11.0 milestone Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH]: Support partial figsize setting

3 participants