mon
mon

Reputation: 22366

Where is the list of available built-in colormap names?

Question

Where in the matplotlib documentations lists the name of available built-in colormap names to set as the name argument in matplotlib.cm.get_cmap(name)?

Choosing Colormaps in Matplotlib says:

Matplotlib has a number of built-in colormaps accessible via matplotlib.cm.get_cmap.

matplotlib.cm.get_cmap says:

matplotlib.cm.get_cmap(name=None, lut=None)
Get a colormap instance, defaulting to rc values if name is None.

  • name: matplotlib.colors.Colormap or str or None, default: None

https://www.kite.com/python/docs/matplotlib.pyplot.colormaps shows multiple names.

autumn  sequential linearly-increasing shades of red-orange-yellow
bone    sequential increasing black-white color map with a tinge of blue, to emulate X-ray film
cool    linearly-decreasing shades of cyan-magenta
copper  sequential increasing shades of black-copper
flag    repetitive red-white-blue-black pattern (not cyclic at endpoints)
gray    sequential linearly-increasing black-to-white grayscale
hot sequential black-red-yellow-white, to emulate blackbody radiation from an object at increasing temperatures
hsv cyclic red-yellow-green-cyan-blue-magenta-red, formed by changing the hue component in the HSV color space
inferno perceptually uniform shades of black-red-yellow
jet a spectral map with dark endpoints, blue-cyan-yellow-red; based on a fluid-jet simulation by NCSA [1]
magma   perceptually uniform shades of black-red-white
pink    sequential increasing pastel black-pink-white, meant for sepia tone colorization of photographs
plasma  perceptually uniform shades of blue-red-yellow
prism   repetitive red-yellow-green-blue-purple-...-green pattern (not cyclic at endpoints)
spring  linearly-increasing shades of magenta-yellow
summer  sequential linearly-increasing shades of green-yellow
viridis perceptually uniform shades of blue-green-yellow
winter  linearly-increasing shades of blue-green

However, simply google 'matplotlib colormap names' seems not hitting the right documentation. I suppose there is a page listing the names as a enumeration or constant strings. Please help find it out.

Upvotes: 0

Views: 5121

Answers (2)

1313e
1313e

Reputation: 1232

You can use my CMasher to make simple colormap overviews of a list of colormaps. In your case, if you want to see what every colormap in MPL looks like, you can use the following:

import cmasher as cmr
import matplotlib.pyplot as plt

cmr.create_cmap_overview(plt.colormaps(), savefig='MPL_cmaps.png')

This will give you an overview with all colormaps that are registered in MPL, which will be all built-in colormaps and all colormaps my CMasher package adds, like shown below: MPL+CMasher colormaps

Upvotes: 1

Zak
Zak

Reputation: 3283

There is some example code in the documentation (thanks to @Patrick Fitzgerald for posting the link in the comments, because it's not half as easy to find as it should be) which demonstrates how to generate a plot with an overview of the installed colormaps.

However, this uses an explicit list of maps, so it's limited to the specific version of matplotlib for which the documentation was written, as maps are added and removed between versions. To see what exactly your environment has, you can use this (somewhat crudely) adapted version of the code:

import numpy as np
import matplotlib.pyplot as plt

gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))

def plot_color_gradients(cmap_category, cmap_list):
    # Create figure and adjust figure height to number of colormaps
    nrows = len(cmap_list)
    figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
    fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
    fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
                        left=0.2, right=0.99)
    axs[0].set_title(cmap_category + ' colormaps', fontsize=14)

    for ax, name in zip(axs, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
                transform=ax.transAxes)

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axs:
        ax.set_axis_off()

cmaps = [name for name in plt.colormaps() if not name.endswith('_r')]
plot_color_gradients('all', cmaps)

plt.show()

This plots just all of them, without regarding the categories. Since plt.colormaps() produces a list of all the map names, this version only removes all the names ending in '_r', (because those are the inverted versions of the other ones), and plots them all. That's still a fairly long list, but you can have a look and then manually update/remove items from cmaps narrow it down to the ones you would consider for a given task.

You can also automatically reduce the list to monochrome/non-monochrome maps, because they provide that properties as an attribute:

cmaps_mono = [name for name in cmaps if plt.get_cmap(name).is_gray()]
cmaps_color = [name for name in cmaps if not plt.get_cmap(name).is_gray()]

That should at least give you a decent starting point. It'd be nice if there was some way within matplotlib to select just certain types of maps (categorical, perceptually uniform, suitable for colourblind viewers ...), but I haven't found a way to do that automatically.

Upvotes: 1

Related Questions