Skip to content
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

added random colormap option to cm.py (lines 705-740ish) #24396

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/matplotlib/cm.py
Expand Up @@ -704,6 +704,14 @@ def changed(self):
)


def get_random_cmap():
"""
Returns a colormap at random from _colormaps

"""
return _get_cmap(np.random.choice(_colormaps))


def _ensure_cmap(cmap):
"""
Ensure that we have a `.Colormap` object.
Expand All @@ -715,6 +723,7 @@ def _ensure_cmap(cmap):
cmap : None, str, Colormap

- if a `Colormap`, return it
- if "random", return a random Colormap with get_random_cmap()
- if a string, look it up in mpl.colormaps
- if None, look up the default color map in mpl.colormaps

Expand All @@ -725,6 +734,8 @@ def _ensure_cmap(cmap):
"""
if isinstance(cmap, colors.Colormap):
return cmap
if cmap == "random":
return get_random_cmap()
cmap_name = cmap if cmap is not None else mpl.rcParams["image.cmap"]
# use check_in_list to ensure type stability of the exception raised by
# the internal usage of this (ValueError vs KeyError)
Expand Down