Skip to content

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

Closed

Description

@glthm

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)

Activity

  1. rcomer commented on Oct 25, 2023

    @rcomer
    Member

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

  2. github-actions commented on Oct 25, 2023

    @github-actions

    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.

  3. added this to the v3.8.1 milestone on Oct 25, 2023
  4. glthm commented on Oct 26, 2023

    @glthm
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions