Skip to content

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

@glthm

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationDocumentation: APIfiles in lib/ and doc/apiGood first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy