Repository navigation
Labels
Milestone
Description
Documentation Link
Problem
levels is specified to be optional but the default value is not specified.
Suggested improvement
Digging into the code, it seems that _process_contour_level_args is responsible? It calls _ensure_locator_exists :
matplotlib/lib/matplotlib/contour.py
Lines 957 to 975 in ebf9b14
| def _ensure_locator_exists(self, N): | |
| """ | |
| Set a locator on this ContourSet if it's not already set. | |
| Parameters | |
| ---------- | |
| N : int or None | |
| If *N* is an int, it is used as the target number of levels. | |
| Otherwise when *N* is None, a reasonable default is chosen; | |
| for logscales the LogLocator chooses, N=7 is the default | |
| otherwise. | |
| """ | |
| if self.locator is None: | |
| if self.logscale: | |
| self.locator = ticker.LogLocator(numticks=N) | |
| else: | |
| if N is None: | |
| N = 7 # Hard coded default | |
| self.locator = ticker.MaxNLocator(N + 1, min_n_ticks=1) |
So does this mean that levels=1 gets passed as MaxNLocator(n_bins=2) which gives max n_bins + 1 (3) levels?
Also in levels it is specified:
If an int n, use MaxNLocator, which tries to automatically choose no more than n+1 "nice" contour levels between minimum and maximum numeric values of Z.
But considering we pass MaxNLocator(N+1), is it n+1 or n+2 ?
Activity
MysteriousCode786 commentedon Jan 21, 2026
Hi @lucyleeow, I am a GSoC 2026 aspirant and I would like to work on this documentation issue. I've gone through the problem description and the code snippet provided. Since no one is assigned yet, could you please assign this to me? I'm excited to make my first contribution to Matplotlib!
timhoffm commentedon Jan 21, 2026
@MysteriousCode786 per our contribution policy, we do not assign issues. You are welcome to directly provide a PR.
timhoffm commentedon Jan 21, 2026
@lucyleeow the minimal and currently implicit description would be
Would that it's that be good enough or is it important for the user to know what happens exactly?
lucyleeow commentedon Jan 21, 2026
Well for my use case, I wanted to know the exact number (ref: scikit-learn/scikit-learn#33108) but we are using it for discrete values as well as integer values, maybe for the general user this is enough?
Also my initial/naive reading of the code seems to suggest there is a hard coded default (7), thus not determined by the data, and thus it may not be 'reasonable' in all cases?
sanrishi commentedon Jan 27, 2026
@lucyleeow yea it is fixed in pr #31016
lucyleeow commentedon Jan 27, 2026
Note to reviewers, with a brief look at the code, it seems to me the default is not 7, but that it will automatically choose maximum of 9 levels. Hopefully a maintainer can confirm what happens when reviewing.
sanrishi commentedon Jan 27, 2026
@lucyleeow Thanks for clarification I implemented the changes to solve the issue
rcomer commentedon Jan 29, 2026
Note there is also the
_autolevmethod, which may trim off some levels that the locator produces. So I think at best we can state what the default levels parameter is, but that does not necessarily translate to the number of levels in your plot.