Skip to content

ENH: Allow contour levels to be decreasing - #32206

Closed
Muhtasim-Munif-Fahim wants to merge 3 commits into
matplotlib:mainfrom
Muhtasim-Munif-Fahim:enh/contour-decreasing-levels
Closed

ENH: Allow contour levels to be decreasing#32206
Muhtasim-Munif-Fahim wants to merge 3 commits into
matplotlib:mainfrom
Muhtasim-Munif-Fahim:enh/contour-decreasing-levels

Conversation

@Muhtasim-Munif-Fahim

Copy link
Copy Markdown

PR summary

Closes #31227

Problem

contour and contourf raise ValueError: Contour levels must be increasing whenever 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

import matplotlib.pyplot as plt
z = [[0, 1], [1, 2]]
plt.contourf(z, levels=[2, 1, 0])  # ValueError: Contour levels must be increasing

What changed

  • When the supplied levels are monotonically decreasing they are reversed internally, so the rest of the code can keep assuming increasing levels. This works for contour, contourf, and their tricontour counterparts, whether the levels are passed as a list, array, or positionally.
  • Per-level styling (colors, linewidths, linestyles, hatches) is reversed together with the levels so that each style stays associated with the level it was given for. When extend is combined with an explicit color list that includes under/over colors, the extended colors are preserved at the ends.
  • Genuinely non-monotonic levels still raise the existing error, so ambiguous orderings are not silently reordered.

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

  • New test_decreasing_levels (contour and contourf, list/array/positional forms).
  • New test_decreasing_levels_styling (colors/linewidths/linestyles stay with their levels).
  • Updated test_contourf_decreasing_levels to 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 both contour and contourf by reversing the levels.

AI Disclosure

N/A

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • Plotting related features are demonstrated in an example (N/A - behavior covered by unit tests)
  • New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

@melissawm melissawm added ai-contribution PRs that are AI generated without a human in the loop status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks labels Aug 12, 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.

@rcomer

rcomer commented Aug 12, 2026

Copy link
Copy Markdown
Member

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.

@jklymak

jklymak commented Aug 12, 2026

Copy link
Copy Markdown
Member

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.

@jklymak jklymak closed this Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-contribution PRs that are AI generated without a human in the loop status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks topic: contour topic: tri

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH]: Allow contour levels to be decreasing

4 participants