Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[MNT]: ListedColormap inconsistencies #28763

Copy link
Copy link
Closed
@oscargus

Description

@oscargus
Issue body actions

Summary

The documentation to ListedColormap states that the input should be a list or array

colors : list, array
Sequence of Matplotlib color specifications (color names or RGB(A)
values).

However, when specifying N, str and float are also supported (if one use the source code)

if isinstance(colors, str):
self.colors = [colors] * N
self.monochrome = True
elif np.iterable(colors):
if len(colors) == 1:
self.monochrome = True
self.colors = list(
itertools.islice(itertools.cycle(colors), N))
else:
try:
gray = float(colors)
except TypeError:
pass
else:
self.colors = [gray] * N
self.monochrome = True

This means that, e.g., ListedColormap("#aabbcc", N=1) works, but ListedColormap("#aabbcc") does not (there will be weird errors later, like N=7 for the latter). Given that there is monochrome attribute and the documentation of N, one may expect the latter to work as well (if the earlier works). Also, ListedColormap(["#aabbcc"]) will not set the monochrome attribute correctly.

Proposed fix

I think there are three(?) possible solutions:

  • Support scalars/strings when N is not provided (and ideally documents that)
  • Document that if N is not None, the first argument can be a scalar or string (it is maybe easier to do ListedColormap(0.3, N=7) than ListedColormap([0.3]*7) or at least require slightly less Python knowledge)
  • Deprecate scalar/string argument

Bonus: document the color and monochrome attributes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.