Matplotlib 简明教程
Matplotlib - Choosing Colormaps
Colormap 也被称为调色板,是一系列颜色,代表了一系列连续的值。它允许您通过颜色的变化有效地表示信息。
Colormap also known as a color table or a palette, is a range of colors that represents a continuous range of values. Allowing you to represent information effectively through color variations.
请参阅下列图片,其中引用了一些 matplotlib 中的内置 colormaps:
See the below image referencing a few built-in colormaps in matplotlib −
Matplotlib 提供了各种内置 colormaps(可以在 matplotlib.colormaps 模块中找到)和第三方 colormaps 以用于各种应用程序。
Matplotlib offers a variety of built-in (available in matplotlib.colormaps module) and third-party colormaps for various applications.
Choosing Colormaps in Matplotlib
选择合适的 colormap 涉及为你的数据集在 3D 颜色空间中找到合适的表示方式。
Choosing an appropriate colormap involves finding a suitable representation in 3D colorspace for your dataset.
选择任何给定数据集的适用 colormap 的因素包括:
The factors for selecting an appropriate colormap for any given data set include −
-
Nature of Data − Whether representing form or metric data
-
Knowledge of the Dataset − Understanding the dataset’s characteristics.
-
Intuitive Color Scheme − Considering if there’s an intuitive color scheme for the parameter being plotted.
-
Field Standards − Considering if there is a standard in the field the audience may be expecting.
对于大多数应用程序,建议使用感知一致的 colormap,确保数据中的相等步长在颜色空间中被感知为相等步长。这改进了人脑解释,特别是当明度变化比色调变化更明显时。
A perceptually uniform colormap is recommended for most of the applications, ensuring equal steps in data are perceived as equal steps in the color space. This improves human brain interpretation, particularly when changes in lightness are more perceptible than changes in hue.
Categories of Colormaps
Colormaps 根据其功能分类:
Colormaps are categorized based on their function −
-
Sequential − Incremental changes in lightness and saturation, often using a single hue. Suitable for representing ordered information.
-
Diverging − Changes in lightness and saturation of two colors, meeting at an unsaturated color. Ideal for data with a critical middle value, like topography or data deviating around zero.
-
Cyclic − Changes in the lightness of two colors, meeting at the middle and beginning/end at an unsaturated color. Suitable for values that wrap around at endpoints, such as phase angle or time of day.
-
Qualitative − Miscellaneous colors with no specific order. Used for representing information without ordering or relationships.
Sequential Colormaps
顺序色图显示出在色图中递增的明度值。这种特性确保了色差感知的平滑过渡,使其适合表示有序信息。然而,值得注意的是,根据它们跨越的值的范围,色图之间的知觉范围可能有所不同。
Sequential colormaps show a monotonically increasing in lightness values as they increase through the colormap. This characteristic ensures a smooth transition in the perception of color changes, making them suitable for representing ordered information. However, it’s essential to note that the perceptual range may vary among colormaps, depending on the range of values they span.
让我们通过可视化渐变并了解其明度值如何演变来探索顺序色图。
Let’s explore Sequential colormaps by visualizing their gradients and understanding how their lightness values evolve.
以下示例提供了 Matplotlib 中可用的不同 Sequential 色图的渐变的可视表示。
The following example provides a visual representation of the gradients for different Sequential colormaps available in Matplotlib.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
# List of Sequential colormaps to visualize
cmap_list = ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']
# Plot the color gradients
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
# Create figure and adjust figure height to the 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=(7, figh))
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
left=0.2, right=0.99)
axs[0].set_title('Sequential colormaps', fontsize=14)
for ax, name in zip(axs, cmap_list):
ax.imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
ax.text(-0.1, 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()
# Show the plot
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Diverging Colormaps
分歧色图的特点是明度值先单调递增,然后再递减,最大明度接近于中性中点。
Diverging colormaps are characterized by monotonically increasing and then decreasing lightness values, with the maximum lightness close to a neutral midpoint.
以下示例提供了 Matplotlib 中可用的不同分歧色图的渐变的可视表示。
The following example provides a visual representation of the gradients for different Diverging colormaps available in Matplotlib.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
# List of Diverging colormaps to visualize
cmap_list = ['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu',
'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']
# Plot the color gradients
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
# Create figure and adjust figure height to the 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=(7, figh))
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
left=0.2, right=0.99)
axs[0].set_title('Diverging colormaps', fontsize=14)
for ax, name in zip(axs, cmap_list):
ax.imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
ax.text(-0.1, 0.5, name, va='center', ha='left', fontsize=10,
transform=ax.transAxes)
# Turn off all ticks & spines, not just the ones with colormaps.
for ax in axs:
ax.set_axis_off()
# Show the plot
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Cyclic Colormaps
循环色图呈现出一种独特的设计,其中色图在同一种颜色上开始和结束,在对称中心点相遇。明度值的进展应从开始到中间单调变化,从中间到结束逆向变化。
Cyclic colormaps present a unique design where the colormap starts and ends on the same color, meeting at a symmetric center point. The progression of lightness values should change monotonically from the start to the middle and inversely from the middle to the end.
在以下示例中,您可以探索和可视化 Matplotlib 中可用的各种循环色图。
In the following example, you can explore and visualize various Cyclic colormaps available in Matplotlib.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
# List of Cyclic colormaps to visualize
cmap_list = ['twilight', 'twilight_shifted', 'hsv']
# Plot the color gradients
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
# Create figure and adjust figure height to the 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=(7, figh))
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
left=0.2, right=0.99)
axs[0].set_title('Cyclic colormaps', fontsize=14)
for ax, name in zip(axs, cmap_list):
ax.imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
ax.text(-0.1, 0.5, name, va='center', ha='left', fontsize=10,
transform=ax.transAxes)
# Turn off all ticks & spines, not just the ones with colormaps.
for ax in axs:
ax.set_axis_off()
# Show the plot
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Qualitative colormaps
定性色图不同于知觉图,因为它们并非设计为提供知觉一致性,而是旨在表示不具有顺序或关系的信息。整个色图中的明度值差异很大,并且不会单调递增。因此,不建议将定性色图用作知觉色图。
Qualitative colormaps are distinct from perceptual maps, as they are not designed to provide a perceptual uniformity but are rather intended for representing information that does not possess ordering or relationships. The lightness values vary widely throughout the colormap and are not monotonically increasing. As a result, Qualitative colormaps are not recommended for use as perceptual colormaps.
以下示例提供了 Matplotlib 中可用的不同定性色图的可视表示。
The following example provides a visual representation of the different Qualitative colormaps available in Matplotlib.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
# List of Qualitative colormaps to visualize
cmap_list = ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2',
'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']
# Plot the color gradients
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
# Create figure and adjust figure height to the 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=(7, figh))
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
left=0.2, right=0.99)
axs[0].set_title('Qualitative colormaps', fontsize=14)
for ax, name in zip(axs, cmap_list):
ax.imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
ax.text(-0.1, 0.5, name, va='center', ha='left', fontsize=10,
transform=ax.transAxes)
# Turn off all ticks & spines, not just the ones with colormaps.
for ax in axs:
ax.set_axis_off()
# Show the plot
plt.show()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −