Skip to content

Small error in docstring of matplotlib.colors.from_levels_and_colors #27188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
glthm opened this issue Oct 25, 2023 · 3 comments · Fixed by #27189
Closed

Small error in docstring of matplotlib.colors.from_levels_and_colors #27188

glthm opened this issue Oct 25, 2023 · 3 comments · Fixed by #27189
Labels
Documentation: API files in lib/ and doc/api Documentation Good first issue Open a pull request against these issues if there are no active ones!
Milestone

Comments

@glthm
Copy link

glthm commented Oct 25, 2023

def from_levels_and_colors(levels, colors, extend='neither'):
"""
A helper routine to generate a cmap and a norm instance which
behave similar to contourf's levels and colors arguments.
Parameters
----------
levels : sequence of numbers
The quantization levels used to construct the `BoundaryNorm`.
Value ``v`` is quantized to level ``i`` if ``lev[i] <= v < lev[i+1]``.
colors : sequence of colors
The fill color to use for each level. If *extend* is "neither" there
must be ``n_level - 1`` colors. For an *extend* of "min" or "max" add
one extra color, and for an *extend* of "both" add two colors.
extend : {'neither', 'min', 'max', 'both'}, optional
The behaviour when a value falls out of range of the given levels.
See `~.Axes.contourf` for details.
Returns
-------
cmap : `~matplotlib.colors.Normalize`
norm : `~matplotlib.colors.Colormap`
"""
slice_map = {
'both': slice(1, -1),
'min': slice(1, None),
'max': slice(0, -1),
'neither': slice(0, None),
}
_api.check_in_list(slice_map, extend=extend)
color_slice = slice_map[extend]
n_data_colors = len(levels) - 1
n_expected = n_data_colors + color_slice.start - (color_slice.stop or 0)
if len(colors) != n_expected:
raise ValueError(
f'With extend == {extend!r} and {len(levels)} levels, '
f'expected {n_expected} colors, but got {len(colors)}')
cmap = ListedColormap(colors[color_slice], N=n_data_colors)
if extend in ['min', 'both']:
cmap.set_under(colors[0])
else:
cmap.set_under('none')
if extend in ['max', 'both']:
cmap.set_over(colors[-1])
else:
cmap.set_over('none')
cmap.colorbar_extend = extend
norm = BoundaryNorm(levels, ncolors=n_data_colors)
return cmap, norm

The docstring of matplotlib.colors.from_levels_and_colors states that the function returns:

    cmap : `~matplotlib.colors.Normalize`
    norm : `~matplotlib.colors.Colormap`

while the code confirms that, as expected, it's the opposite that is returned (cmap is a Colormap and norm is a Normalize)

@rcomer
Copy link
Member

rcomer commented Oct 25, 2023

Good spot @glthm! Are you interested in making a pull request to correct it?

@rcomer rcomer added Documentation Documentation: API files in lib/ and doc/api Good first issue Open a pull request against these issues if there are no active ones! labels Oct 25, 2023
@github-actions
Copy link

Good first issue - notes for new contributors

This issue is suited to new contributors because it does not require understanding of the Matplotlib internals. To get started, please see our contributing guide.

We do not assign issues. Check the Development section in the sidebar for linked pull requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please collaborate on the work by reviewing it rather than duplicating it in a competing PR.

If something is unclear, please reach out on any of our communication channels.

@glthm
Copy link
Author

glthm commented Oct 26, 2023

someone was faster than me, my first pull request won't be for today, maybe next time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: API files in lib/ and doc/api Documentation Good first issue Open a pull request against these issues if there are no active ones!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants