SoFunction
Updated on 2024-11-16

Python how to view and print all colormap (cmap) types in matplotlib

View and print all colormap (cmap) types in matplotlib

The code is as follows:

Method 1

import  as plt

cmaps = sorted(m for m in  if not ("_r"))
print(cmaps)

We ignore types ending in _r because they are reversed versions of types without _r after them.

All the types we can find in the source code of matplotlib: (below)

Method II

import  as plt

cmap_list1 = ()
print(cmap_list1)

Method III

If you are using the Pycharm compiler, then you can simply give a random cmap type when diagramming, and if the given cmap type is wrong, then all the cmap types will also be shown in the compiler's error message.

For example, we here we want to make a Gaussian function of the surface distribution graph, we arbitrarily give cmap a value of 'aaa', at this point, we can see the following error message output in the compiler prompt window.

import numpy as np
import  as plt
from mpl_toolkits.mplot3d import Axes3D

x = (-3, 3, 100)
y = (-3, 3, 100)
x, y = (x, y)
w0 = 1
gaussian = (-((pow(x, 2) + pow(y, 2)) / pow(w0, 2)))

fig = ()
ax = Axes3D(fig)
ax.plot_surface(x, y, gaussian, cmap='aaa')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
()
"""
error message:.
ValueError: 'aaa' is not a valid value for name; supported values are 'Accent',
 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu',
 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens',
 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r',
 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2',
 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr',
 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy',
 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds',
 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral',
 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r',
 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn',
 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr',
 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r',
 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r','gist_earth',
 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat','gist_heat_r', 'gist_ncar',
 'yeast_ncar_r', 'yeast_rainbow', 'yeast_rainbow_r','yeast_stern', 'yeast_stern_r',
 'gist_yarg', 'gist_yarg_r', 'gnuplot','gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray',
 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet','jet_r',
 'magma', 'magma_r','nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r',
'pink', 'pink_r','plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r',
'seismic','seismic_r','spring','spring_r','summer','summer_r','tab10','tab10_r',
'tab20','tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain','terrain_r',
'turbo','turbo_r', 'twilight','twilight_r','twilight_shifted','twilight_shifted_r',
'viridis','viridis_r','winter','winter_r'.
"""

matplotlib cmap fetch problem

Define a class directly to get the individual colors in the cmap for ease of use

To use: mycolor = MyColor('Accent'); mycolor.get_color(); # Call it every time to get the color in the next cmap.

class MyColor(object):
    def __init__(self, cmap_name):
        self.color_set  = plt.get_cmap(cmap_name).colors
         = 0
        self.color_len = len(self.color_set)
        
    def get_color(self):
        if  == self.color_len - 1:
             = 0
        color = self.color_set[]
         += 1
        return color

Visualize the officially provided cmap

For example, see: ['Pastel1', 'Pastel2', 'Paired', 'Accent ', 'Dark2', 'Set1', 'Set2', 'Set3 ', 'tab10', 'tab20', 'tab20b', 'tab20c ']

import  as plt
import numpy as np
import  as plt

cmaps = {}
gradient = (0, 1, 256)
gradient = ((gradient, gradient))

def plot_color_gradients(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 = (nrows=nrows + 1, figsize=(6.4, figh), dpi=100)
    fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
                        left=0.2, right=0.99)
    axs[0].set_title(f'{category} colormaps', fontsize=14)

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

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

    # Save colormap list for later.
    cmaps[category] = cmap_list
    

plot_color_gradients('Qualitative',
                     ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
                      'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b',
                      'tab20c'])

After the run:

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.