Repository navigation
Small error in docstring of matplotlib.colors.from_levels_and_colors #27188
Copy link
Copy link
Closed
Labels
DocumentationDocumentation: APIfiles in lib/ and doc/apifiles in lib/ and doc/api🌱 Good first issueOpen a pull request against these issues if there are no active ones!Open a pull request against these issues if there are no active ones!
Milestone
Description
matplotlib/lib/matplotlib/colors.py
Lines 2708 to 2762 in 3958242
| 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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DocumentationDocumentation: APIfiles in lib/ and doc/apifiles in lib/ and doc/api🌱 Good first issueOpen a pull request against these issues if there are no active ones!Open a pull request against these issues if there are no active ones!
Activity
rcomer commentedon Oct 25, 2023
Good spot @glthm! Are you interested in making a pull request to correct it?
github-actions commentedon Oct 25, 2023
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 commentedon Oct 26, 2023
someone was faster than me, my first pull request won't be for today, maybe next time