ENH: Allow contour levels to be decreasing - #32206
Conversation
|
⏰ 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 |
|
There is actually already a PR for this, although it was not properly linked to the issue #31237. There is a lot of discussion in both the issue and the previous PR, which should be accounted for in any new PR. |
|
I'll actually close this. While I don't intend to move forward on #31237 because of what I consider needless restrictions on the API, the solution here is way too complicated and very much the wrong track. |
PR summary
Closes #31227
Problem
contourandcontourfraiseValueError: Contour levels must be increasingwhenever the contour levels are given in monotonically decreasing order. This prevents the use case described in #31227: plotting a fixed set of negative contours (e.g.-base * 2**arange(...)grids) that extends beyond the data range, where the value closest to zero is known ahead of time but the depth of the data is not.Reproduction
What changed
contour,contourf, and theirtricontourcounterparts, whether the levels are passed as a list, array, or positionally.colors,linewidths,linestyles,hatches) is reversed together with the levels so that each style stays associated with the level it was given for. Whenextendis combined with an explicit color list that includes under/over colors, the extended colors are preserved at the ends.Why this approach
This follows the direction discussed in the issue: the maintainers rejected sorting arbitrary user level lists (ambiguous for per-level colors), but a monotonic decreasing list can be unambiguously reversed, and the per-level attributes flipped with it.
ContourSet.levels,layers, and the colorbar therefore stay consistent with the rest of the code.How it was tested
test_decreasing_levels(contour and contourf, list/array/positional forms).test_decreasing_levels_styling(colors/linewidths/linestyles stay with their levels).test_contourf_decreasing_levelsto cover the still-rejected non-monotonic case.python -m pytest lib/matplotlib/tests/test_contour.py -v-> 92 passed, 2 skipped.Related work
PR #31237 addresses the same issue with a different approach: it removes the monotonicity requirement for line contours entirely (accepting arbitrary, including non-monotonic, orders without reordering) while keeping the error for
contourf. This PR instead handles the strictly decreasing case for bothcontourandcontourfby reversing the levels.AI Disclosure
N/A
PR quality check