在 Matplotlib 中选择颜色表#

Matplotlib 有许多内置的颜色映射可以通过 matplotlib.colormaps 访问。还有一些外部库提供了许多额外的颜色映射,可以在 Matplotlib 文档的 第三方颜色映射 部分查看。这里我们简要讨论如何在众多选项中进行选择。关于创建自己的颜色映射的帮助,请参见 颜色映射操作

要获取所有已注册的颜色映射列表,您可以执行:

from matplotlib import colormaps
list(colormaps)

概述#

选择一个好的色图背后的想法是为你的数据集在三维颜色空间中找到一个好的表示。任何给定数据集的最佳色图取决于许多因素,包括:

  • 无论是表示形式数据还是度量数据 ([Ware])

  • 你对数据集的了解(例如,是否存在一个临界值,其他值从此值开始偏离?)

  • 如果你正在绘制的参数有一个直观的颜色方案

  • 如果该领域有标准,观众可能会期待

对于许多应用来说,感知均匀的色图是最佳选择;即在数据中相等的步长在颜色空间中被感知为相等的步长。研究人员发现,人脑对亮度参数的变化感知为数据的变化,远比对色调的变化感知要好。因此,在整个色图中亮度单调增加的色图将更容易被观众解读。在 第三方色图 部分中可以找到许多感知均匀色图的优秀示例。

颜色可以在三维空间中以多种方式表示。一种表示颜色的方法是使用 CIELAB。在 CIELAB 中,颜色空间由明度 \(L^*\);红-绿 \(a^*\);和黄-蓝 \(b^*\) 表示。明度参数 \(L^*\) 可以用来了解更多关于 matplotlib 色图将如何被观众感知的信息。

学习关于人类对色彩映射感知的一个优秀起点资源来自 [IBM]

颜色映射的类别#

色图通常根据其功能分为几类(参见,例如,[Moreland]):

  1. 顺序:颜色的亮度和通常饱和度的逐步变化,通常使用单一色调;应用于表示具有顺序的信息。

  2. 发散:两种不同颜色在中间以不饱和颜色相遇时,亮度和可能的饱和度的变化;当绘制的信息具有关键的中间值(如地形)或数据围绕零偏离时,应使用此方法。

  3. 循环:两种不同颜色在中间相遇并开始/结束于不饱和颜色的亮度变化;应用于在端点处环绕的值,例如相位角、风向或一天中的时间。

  4. 定性:通常是杂色;应用于表示没有顺序或关系的信息。

from colorspacious import cspace_converter

import matplotlib.pyplot as plt
import numpy as np

import matplotlib as mpl

首先,我们将展示每个色图的范围。注意,有些色图似乎比其他的“变化更快”。

cmaps = {}

gradient = np.linspace(0, 1, 256)
gradient = np.vstack((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 = 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(f'{category} colormaps', fontsize=14)

    for ax, name in zip(axs, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=mpl.colormaps[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()

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

顺序#

对于顺序图,颜色映射中的亮度值单调递增。这是好的。一些颜色映射中的 \(L^*\) 值从 0 到 100(二进制和其他灰度),而其他颜色映射从大约 \(L^*=20\) 开始。那些 \(L^*\) 范围较小的颜色映射相应地具有较小的感知范围。还要注意,颜色映射之间的 \(L^*\) 函数有所不同:一些在 \(L^*\) 上近似线性,而其他则更为弯曲。

plot_color_gradients('Perceptually Uniform Sequential',
                     ['viridis', 'plasma', 'inferno', 'magma', 'cividis'])
Perceptually Uniform Sequential colormaps
plot_color_gradients('Sequential',
                     ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
                      'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
                      'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'])
Sequential colormaps

Sequential2#

许多来自 Sequential2 图的 \(L^*\) 值是单调递增的,但有些(秋季、凉爽、春季和冬季)在 \(L^*\) 空间中达到平台或甚至上下波动。其他(afmhot、铜色、gist_heat 和 hot)在 \(L^*\) 函数中有拐点。在色图的平坦区域或拐点区域表示的数据将导致在这些色图值中对数据的分带感知(参见 [mycarta-banding] 中的一个优秀示例)。

plot_color_gradients('Sequential (2)',
                     ['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone',
                      'pink', 'spring', 'summer', 'autumn', 'winter', 'cool',
                      'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper'])
Sequential (2) colormaps

分歧#

对于发散地图,我们希望在达到接近 \(L^*=100\) 的最大值之前, \(L^*\) 值是单调递增的,然后是单调递减的 \(L^*\) 值。我们正在寻找在色图两端大约相等的最小 \(L^*\) 值。根据这些标准,BrBG 和 RdBu 是不错的选择。coolwarm 也是一个好选项,但它不涵盖广泛的 \(L^*\) 值范围(参见下面的灰度部分)。

柏林、马那瓜和瓦尼莫是暗模式发散色图,中心处亮度最低,两端亮度最高。这些色图来自F. Crameri的[科学色彩图谱]_版本8.0.1。

plot_color_gradients('Diverging',
                     ['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu',
                      'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic',
                      'berlin', 'managua', 'vanimo'])
Diverging colormaps

循环#

对于循环地图,我们希望从相同的颜色开始和结束,并在中间遇到一个对称的中心点。\(L^*\) 应从开始到中间单调变化,从中间到结束反向变化。它应在增加和减少的两边对称,仅在色调上有所不同。在两端和中间,\(L^*\) 将改变方向,这应在 \(L^*\) 空间中平滑处理以减少伪影。更多关于循环地图设计的信息,请参见 [kovesi-colormaps]

虽然这个色图集中包含了常用的HSV色图,但它并不是围绕中心点对称的。此外,色图中的 \(L^*\) 值在整个色图中变化很大,这使得它不适合用于代表数据以供观众感知。关于这个想法的扩展,请参见 [mycarta-jet]

plot_color_gradients('Cyclic', ['twilight', 'twilight_shifted', 'hsv'])
Cyclic colormaps

定性#

定性色图的目的不是作为感知图,但查看亮度参数可以为我们验证这一点。\(L^*\) 值在整个色图中到处移动,并且显然不是单调递增的。这些不适合作为感知色图使用。

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

杂项#

一些杂项色图有其特定的用途。例如,gist_earth、ocean 和 terrain 似乎都是为绘制地形(绿色/棕色)和水深(蓝色)而创建的。我们可能会在这些色图中看到差异,但多个拐点可能并不理想,例如在 gist_earth 和 terrain 中。CMRmap 是为转换为灰度而创建的,尽管它在 \(L^*\) 中似乎有一些小的拐点。cubehelix 是为在亮度和色调上平滑变化而创建的,但在绿色色调区域似乎有一个小凸起。turbo 是为显示深度和视差数据而创建的。

经常使用的喷射颜色图包含在这一组颜色图中。我们可以看到,颜色图中的 \(L^*\) 值变化很大,这使得它不适合用来表示观众感知的数据。关于这一想法的扩展,请参见 [mycarta-jet][turbo]

plot_color_gradients('Miscellaneous',
                     ['flag', 'prism', 'ocean', 'gist_earth', 'terrain',
                      'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap',
                      'cubehelix', 'brg', 'gist_rainbow', 'rainbow', 'jet',
                      'turbo', 'nipy_spectral', 'gist_ncar'])

plt.show()
Miscellaneous colormaps

Matplotlib 色彩映射的亮度#

在这里,我们检查了 matplotlib 色图的亮度值。请注意,有关色图的一些文档是可用的 ([list-colormaps]).

mpl.rcParams.update({'font.size': 12})

# Number of colormap per subplot for particular cmap categories
_DSUBS = {'Perceptually Uniform Sequential': 5, 'Sequential': 6,
          'Sequential (2)': 6, 'Diverging': 6, 'Cyclic': 3,
          'Qualitative': 4, 'Miscellaneous': 6}

# Spacing between the colormaps of a subplot
_DC = {'Perceptually Uniform Sequential': 1.4, 'Sequential': 0.7,
       'Sequential (2)': 1.4, 'Diverging': 1.4, 'Cyclic': 1.4,
       'Qualitative': 1.4, 'Miscellaneous': 1.4}

# Indices to step through colormap
x = np.linspace(0.0, 1.0, 100)

# Do plot
for cmap_category, cmap_list in cmaps.items():

    # Do subplots so that colormaps have enough space.
    # Default is 6 colormaps per subplot.
    dsub = _DSUBS.get(cmap_category, 6)
    nsubplots = int(np.ceil(len(cmap_list) / dsub))

    # squeeze=False to handle similarly the case of a single subplot
    fig, axs = plt.subplots(nrows=nsubplots, squeeze=False,
                            figsize=(7, 2.6*nsubplots))

    for i, ax in enumerate(axs.flat):

        locs = []  # locations for text labels

        for j, cmap in enumerate(cmap_list[i*dsub:(i+1)*dsub]):

            # Get RGB values for colormap and convert the colormap in
            # CAM02-UCS colorspace.  lab[0, :, 0] is the lightness.
            rgb = mpl.colormaps[cmap](x)[np.newaxis, :, :3]
            lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)

            # Plot colormap L values.  Do separately for each category
            # so each plot can be pretty.  To make scatter markers change
            # color along plot:
            # https://stackoverflow.com/q/8202605/

            if cmap_category == 'Sequential':
                # These colormaps all start at high lightness, but we want them
                # reversed to look nice in the plot, so reverse the order.
                y_ = lab[0, ::-1, 0]
                c_ = x[::-1]
            else:
                y_ = lab[0, :, 0]
                c_ = x

            dc = _DC.get(cmap_category, 1.4)  # cmaps horizontal spacing
            ax.scatter(x + j*dc, y_, c=c_, cmap=cmap, s=300, linewidths=0.0)

            # Store locations for colormap labels
            if cmap_category in ('Perceptually Uniform Sequential',
                                 'Sequential'):
                locs.append(x[-1] + j*dc)
            elif cmap_category in ('Diverging', 'Qualitative', 'Cyclic',
                                   'Miscellaneous', 'Sequential (2)'):
                locs.append(x[int(x.size/2.)] + j*dc)

        # Set up the axis limits:
        #   * the 1st subplot is used as a reference for the x-axis limits
        #   * lightness values goes from 0 to 100 (y-axis limits)
        ax.set_xlim(axs[0, 0].get_xlim())
        ax.set_ylim(0.0, 100.0)

        # Set up labels for colormaps
        ax.xaxis.set_ticks_position('top')
        ticker = mpl.ticker.FixedLocator(locs)
        ax.xaxis.set_major_locator(ticker)
        formatter = mpl.ticker.FixedFormatter(cmap_list[i*dsub:(i+1)*dsub])
        ax.xaxis.set_major_formatter(formatter)
        ax.xaxis.set_tick_params(rotation=50)
        ax.set_ylabel('Lightness $L^*$', fontsize=12)

    ax.set_xlabel(cmap_category + ' colormaps', fontsize=14)

    fig.tight_layout(h_pad=0.0, pad=1.5)
    plt.show()
  • colormaps
  • colormaps
  • colormaps
  • colormaps
  • colormaps
  • colormaps
  • colormaps

灰度转换#

对于彩色图表,转换为灰度图非常重要,因为它们可能会在黑白打印机上打印。如果不仔细考虑,你的读者可能会得到无法辨认的图表,因为灰度在色图中变化不可预测。

灰度转换有多种不同的方法 [bw]。其中一些较好的方法使用像素的rgb值的线性组合,但根据我们对颜色强度的感知进行加权。一种非线性的灰度转换方法是使用像素的 \(L^*\) 值。通常,对于这个问题,呈现信息感知的原则与此类似;也就是说,如果选择了一个在 \(L^*\) 值上单调递增的色图,它将以合理的方式打印为灰度。

考虑到这一点,我们看到 Sequential 色图在灰度中有合理的表示。一些 Sequential2 色图在灰度表示中相当不错,尽管有些(autumn, spring, summer, winter)在灰度变化上非常小。如果像这样的色图被用于绘图,然后该绘图被打印为灰度,很多信息可能会映射到相同的灰度值。Diverging 色图大多从边缘的深灰色变化到中间的白色。一些(PuOr 和 seismic)在一侧比另一侧有明显更深的灰色,因此不是很对称。coolwarm 的灰度范围很小,打印时会形成更均匀的图,丢失很多细节。请注意,叠加的带标签等高线可以帮助区分色图的一侧与另一侧,因为一旦图被打印为灰度,颜色就无法使用。许多定性和杂项色图,如 Accent、hsv、jet 和 turbo,在整个色图中从深灰色变为浅灰色再变回深灰色。这将使得观众在图被打印为灰度后无法解释图中的信息。

mpl.rcParams.update({'font.size': 14})

# Indices to step through colormap.
x = np.linspace(0.0, 1.0, 100)

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


def plot_color_gradients(cmap_category, cmap_list):
    fig, axs = plt.subplots(nrows=len(cmap_list), ncols=2)
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99,
                        wspace=0.05)
    fig.suptitle(cmap_category + ' colormaps', fontsize=14, y=1.0, x=0.6)

    for ax, name in zip(axs, cmap_list):

        # Get RGB values for colormap.
        rgb = mpl.colormaps[name](x)[np.newaxis, :, :3]

        # Get colormap in CAM02-UCS colorspace. We want the lightness.
        lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)
        L = lab[0, :, 0]
        L = np.float32(np.vstack((L, L, L)))

        ax[0].imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
        ax[1].imshow(L, aspect='auto', cmap='binary_r', vmin=0., vmax=100.)
        pos = list(ax[0].get_position().bounds)
        x_text = pos[0] - 0.01
        y_text = pos[1] + pos[3]/2.
        fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)

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

    plt.show()


for cmap_category, cmap_list in cmaps.items():

    plot_color_gradients(cmap_category, cmap_list)
  • Perceptually Uniform Sequential colormaps
  • Sequential colormaps
  • Sequential (2) colormaps
  • Diverging colormaps
  • Cyclic colormaps
  • Qualitative colormaps
  • Miscellaneous colormaps

色觉缺陷#

关于色盲的信息有很多(例如,[colorblindness])。此外,还有一些工具可以将图像转换为不同类型色觉缺陷者看到的样子。

最常见的色觉缺陷形式涉及区分红色和绿色。因此,避免使用同时包含红色和绿色的色图将避免许多一般性问题。

参考文献#

脚本总运行时间: (0 分钟 4.890 秒)

由 Sphinx-Gallery 生成的图库