Skip to content

FIX: fill contourf minimum region when it falls just below the lowest level (#21382)#31903

Open
SharadhNaidu wants to merge 1 commit into
matplotlib:mainfrom
SharadhNaidu:fix-21382-contourf-zmin-tolerance
Open

FIX: fill contourf minimum region when it falls just below the lowest level (#21382)#31903
SharadhNaidu wants to merge 1 commit into
matplotlib:mainfrom
SharadhNaidu:fix-21382-contourf-zmin-tolerance

Conversation

@SharadhNaidu

@SharadhNaidu SharadhNaidu commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Closes #21382.

PR summary

this PR fixes an annoying edge case where contourf leaves the lowest contour region blank due to standard floating point right now ContourSet._get_lowers_and_uppers requires self.zmin to exactly equal the lowest level meaning a computed minimum that's just a tiny margin like -1.7e-13 instead of 0 gets clipped instead of drawn

to fix this i replaced the exact == check with a tolerance scaled to the data range i only applied this to linear scales as log scales cover way too many orders of magnitude for an additive tolerance to mean anything the new check is one sided ensuring that deliberate gaps like user passed levels starting above the data are perfectly preserved while quietly catching the rounding errors the old check used to miss

showcase_21382

I used AI to help me understand parts of the codebase in the contour code part . The code changes and design decisions were developed independently.

Testing

Added a check_figures_equal test that a float-noise minimum draws the same as a clean zero (including at very small and very large data scales), plus guard tests so a user-specified lowest level above the data doesn't get back-filled even when a big data range inflates the tolerance.

pytest lib/matplotlib/tests/test_contour.py

PR checklist

  • The changes are tested.
  • The issue is referenced.

… level (matplotlib#21382)

contourf left the minimum-valued region unfilled when the data minimum
fell a floating-point hair below the lowest contour level instead of
being exactly equal to it (e.g. a computed -1.7e-13 where 0 was meant).
ContourSet._get_lowers_and_uppers only extended the lowest filled
interval down to the minimum when self.zmin == lowers[0] exactly, so
such near-equal minima, or rounding during automatic level selection,
slipped through and the region was clipped.

On a linear scale, compare the level against the data minimum with a
tolerance scaled to the data range rather than strict equality, the same
way Colorbar._add_solids does. The check is one-sided and tolerance-
bounded, so genuine gaps such as user-specified levels starting above
the data are left untouched, and every case the old equality test caught
still extends identically. Log scales span many decades where an additive
tolerance is not meaningful, so the exact-equality test is kept there
unchanged.

Adds check_figures_equal regression tests that a float-noise minimum
renders like a clean zero, including at very small and very large data
scales, plus guard tests that a user-specified lowest level above the
data is not back-filled, even when a large data range inflates the
tolerance to order unity. Includes a behaviour-change note under
doc/api/next_api_changes/.
@SharadhNaidu SharadhNaidu force-pushed the fix-21382-contourf-zmin-tolerance branch from 4f525d1 to d70e9b9 Compare June 15, 2026 11:01
@QuLogic

QuLogic commented Jun 16, 2026

Copy link
Copy Markdown
Member

I used AI ... drafting this PR description.

Please don't do that; we want your description of what you did, and AI's writing is tiring to read.

@SharadhNaidu

Copy link
Copy Markdown
Contributor Author

i am really sorry , i will make sure to not use AI for PR description in future , please give me some time , i will update the PR description .

@SharadhNaidu

SharadhNaidu commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

@QuLogic I am really sorry for the inconvenience , updated and ready.

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.

[Bug]: Data 0 cannot be plotted by matplotlib.pyplot just because some data is less than 0.

2 participants