Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view all colormaps available in matplotlib?

I was wondering if there is a function call that can give me the name of all colormaps available in matplotlib?

It used to be possible by something along the lines of (see here):

import matplotlib.pyplot as plt
cmaps = sorted(m for m in plt.cm.datad if not m.endswith("_r"))

But running this in mpl 1.5 does not return the new colormaps, such as viridis, magma and so on. On the new reference page the code actually hardcodes the names (see here) but I was wondering if a similar query to the above is still possible?

like image 592
lab Avatar asked Dec 16 '15 14:12

lab


People also ask

What is a Matplotlib colormap?

Scientifically, the human brain perceives various intuition based on the different colors they see. Matplotlib provides some nice colormaps you can use, such as Sequential colormaps, Diverging colormaps, Cyclic colormaps, and Qualitative colormaps.

What is Matplotlib default colormap?

Colormap. The new default colormap used by matplotlib. cm. ScalarMappable instances is 'viridis' (aka option D).

What is CMAP viridis?

( cmaps.viridis is a matplotlib.colors.ListedColormap ) import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np import colormaps as cmaps img=mpimg.imread('stinkbug.png') lum_img = np.flipud(img[:,:,0]) imgplot = plt.pcolormesh(lum_img, cmap=cmaps.viridis)


2 Answers

plt.colormaps() returns a list of all registered colormaps. From the docs:

matplotlib.pyplot.colormaps()

Matplotlib provides a number of colormaps, and others can be added using register_cmap(). This function documents the built-in colormaps, and will also return a list of all registered colormaps if called.

The list this returns includes viridis, magma, inferno and plasma for me in 1.5.0

like image 143
tmdavison Avatar answered Sep 23 '22 00:09

tmdavison


Here's some code that plots all available colormaps linked to their ID's

import matplotlib as mpl
import matplotlib.pyplot as plt

def plot_colorMaps(cmap):

    fig, ax = plt.subplots(figsize=(4,0.4))
    col_map = plt.get_cmap(cmap)
    mpl.colorbar.ColorbarBase(ax, cmap=col_map, orientation = 'horizontal')

    plt.show()

for cmap_id in plt.colormaps():
    print(cmap_id)
    plot_colorMaps(cmap_id)

The output looks like this

Accent

enter image description here

Accent_r

enter image description here

Blues

enter image description here

etc...

like image 44
pr94 Avatar answered Sep 26 '22 00:09

pr94