Selecting individual colors from a colormap#

Sometimes we want to use more colors or a different set of colors than the default color cycle provides. Selecting individual colors from one of the provided colormaps can be a convenient way to do this.

We can retrieve colors from any Colormap by calling it with a float or a list of floats in the range [0, 1]; e.g. cmap(0.5) will give the middle color. See also Colormap.__call__.

Extracting colors from a continuous colormap#

import matplotlib.pyplot as plt
import numpy as np

import matplotlib as mpl

n_lines = 21
cmap = mpl.colormaps['plasma']

# Take colors at regular intervals spanning the colormap.
colors = cmap(np.linspace(0, 1, n_lines))

fig, ax = plt.subplots(layout='constrained')

for i, color in enumerate(colors):
    ax.plot([0, i], color=color)

plt.show()
individual colors from cmap

Extracting colors from a discrete colormap#

The list of all colors in a ListedColormap is available as the colors attribute. Note that all the colors from Matplotlib's qualitative color maps are also available as color sequences, so may be accessed more directly from the color sequence registry. See Named color sequences.

colors = mpl.colormaps['Dark2'].colors

fig, ax = plt.subplots(layout='constrained')

for i, color in enumerate(colors):
    ax.plot([0, i], color=color)

plt.show()
individual colors from cmap

See Also#

For more details about manipulating colormaps, see Creating Colormaps in Matplotlib. To change the default color cycle, see Styling with cycler.

References

The use of the following functions, methods, classes and modules is shown in this example:

Tags: styling: colormap styling: color plot-type: line level: intermediate

Total running time of the script: (0 minutes 1.407 seconds)

Gallery generated by Sphinx-Gallery

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