Skip to content

[Doc]: contour and contourf levels default not specified #30996

Description

@lucyleeow

Documentation Link

https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.contourf.html

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 :

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

  1. MysteriousCode786 commented on Jan 21, 2026

    @MysteriousCode786
    Contributor

    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!

  2. timhoffm commented on Jan 21, 2026

    @timhoffm
    Member

    @MysteriousCode786 per our contribution policy, we do not assign issues. You are welcome to directly provide a PR.

  3. timhoffm commented on Jan 21, 2026

    @timhoffm
    Member

    @lucyleeow the minimal and currently implicit description would be

    If not given, a reasonable number of levels is automatically chosen.

    Would that it's that be good enough or is it important for the user to know what happens exactly?

  4. lucyleeow commented on Jan 21, 2026

    @lucyleeow
    ContributorAuthor

    Would that it's that be good enough or is it important for the user to know what happens exactly?

    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?

  5. sanrishi commented on Jan 27, 2026

    @sanrishi
    Contributor

    @lucyleeow yea it is fixed in pr #31016

  6. lucyleeow commented on Jan 27, 2026

    @lucyleeow
    ContributorAuthor

    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.

  7. sanrishi commented on Jan 27, 2026

    @sanrishi
    Contributor

    @lucyleeow Thanks for clarification I implemented the changes to solve the issue

  8. rcomer commented on Jan 29, 2026

    @rcomer
    Member

    Note there is also the _autolev method, 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.

  9. added this to the v3.11.0 milestone on Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions