Skip to content

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

Open
Vikash-Kumar-23 wants to merge 2 commits intomatplotlib:mainfrom
Vikash-Kumar-23:figsize-partial-support
Open

ENH: Support partial figsize with None (#31400)#31413
Vikash-Kumar-23 wants to merge 2 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.

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

2 participants