从颜色映射中选择单个颜色#

有时我们想要使用比默认颜色循环提供的更多颜色或不同的颜色集.从提供的颜色映射中选择单个颜色可能是一种方便的方法.

我们可以通过使用范围 [0, 1] 中的浮点数或浮点数列表调用任何 Colormap 来检索颜色; 例如 cmap(0.5) 将给出中间颜色.另请参见 Colormap.__call__ .

从连续颜色映射中提取颜色#

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

从离散颜色映射中提取颜色#

ListedColormap 中所有颜色的列表可作为 colors 属性使用.

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

参见#

有关操作颜色映射的更多详细信息,请参阅 在 Matplotlib 中创建颜色映射 . 要更改默认颜色循环,请参阅 使用 cycler 设置样式 .

参考

以下函数,方法,类和模块的用法在本例中显示:

  • matplotlib.colors.Colormap

  • matplotlib.colors.Colormap.resampled

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

Gallery generated by Sphinx-Gallery