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

Skip to content

Fix contour behavior when Z is constant (zmin == zmax)#31534

Open
manasvi-sahare wants to merge 1 commit intomatplotlib:mainfrom
manasvi-sahare:fix-contour-constant-z-31493
Open

Fix contour behavior when Z is constant (zmin == zmax)#31534
manasvi-sahare wants to merge 1 commit intomatplotlib:mainfrom
manasvi-sahare:fix-contour-constant-z-31493

Conversation

@manasvi-sahare
Copy link
Copy Markdown

Fixes #31493.

This PR addresses an issue where contour/contourf produces invalid or misleading output when the input data is constant (i.e., zmin == zmax).

Problem

When Z is constant, the automatic level generation in _autolev passes identical values to the locator, which results in invalid or non-informative contour levels.

Changes

  • Added a guard in _autolev to detect the case zmin == zmax
  • Emit a UserWarning indicating that contour levels are not meaningful for constant data
  • Return a single valid contour level ([zmin]) to prevent downstream errors

Behavior

  • No crash occurs for constant input data
  • A warning is issued to inform the user
  • Contour generation remains stable and predictable

Testing

Added a test case in test_contour.py to verify:

  • Warning is raised for constant Z
  • Only one contour level is generated

@melissawm
Copy link
Copy Markdown
Member

Hi @manasvi-sahare , please update the PR description to correctly include the filled PR template, especially the AI disclosure section. Thanks

@rcomer rcomer added the status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks label Apr 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks topic: contour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: contour/contourf silently produces nonsense levels when Z is constant (zmin == zmax)

4 participants