备注
前往末尾 下载完整示例代码。
颜色映射归一化#
默认情况下,使用颜色映射的对象会从数据值 vmin 到 vmax 线性映射颜色映射中的颜色。例如:
pcm = ax.pcolormesh(x, y, Z, vmin=-1., vmax=1., cmap='RdBu_r')
将 Z 中的数据线性映射从 -1 到 +1,因此 Z=0 将给出颜色映射 RdBu_r 中心的颜色(在这种情况下为白色)。
Matplotlib 通过两个步骤进行这种映射,首先将输入数据归一化到 [0, 1],然后将数据映射到颜色映射中的索引。归一化是在 matplotlib.colors() 模块中定义的类。默认的线性归一化是 matplotlib.colors.Normalize()。
将数据映射到颜色的艺术家通过传递参数 vmin 和 vmax 来构建一个 matplotlib.colors.Normalize() 实例,然后调用它:
>>> import matplotlib as mpl
>>> norm = mpl.colors.Normalize(vmin=-1, vmax=1)
>>> norm(0)
0.5
然而,有时将数据非线性地映射到色图是有用的。
对数#
最常见的变换之一是通过取其对数(以10为底)来绘制数据。这种变换有助于显示跨越不同尺度的变化。使用 colors.LogNorm 通过 \(log_{10}\) 对数据进行归一化。在下面的示例中,有两个凸起,其中一个比另一个小得多。使用 colors.LogNorm,可以清楚地看到每个凸起的形状和位置:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
import matplotlib.cbook as cbook
import matplotlib.colors as colors
N = 100
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
# A low hump with a spike coming out of the top right. Needs to have
# z/colour axis on a log scale, so we see both hump and spike. A linear
# scale only shows the spike.
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X * 10)**2 - (Y * 10)**2)
Z = Z1 + 50 * Z2
fig, ax = plt.subplots(2, 1)
pcm = ax[0].pcolor(X, Y, Z,
norm=colors.LogNorm(vmin=Z.min(), vmax=Z.max()),
cmap='PuBu_r', shading='auto')
fig.colorbar(pcm, ax=ax[0], extend='max')
pcm = ax[1].pcolor(X, Y, Z, cmap='PuBu_r', shading='auto')
fig.colorbar(pcm, ax=ax[1], extend='max')
plt.show()

居中#
在许多情况下,数据围绕一个中心对称,例如,围绕中心0的正负异常。在这种情况下,我们希望中心映射到0.5,并且与中心偏差最大的数据点映射到1.0(如果其值大于中心)或0.0(否则)。范数 colors.CenteredNorm 自动创建这种映射。它非常适合与发散色图结合使用,该色图使用在中心以不饱和颜色相遇的不同颜色边缘。
如果对称中心不同于0,可以使用 vcenter 参数设置。对于中心两侧的对数缩放,请参见下面的 colors.SymLogNorm;要在中心上方和下方应用不同的映射,请使用下面的 colors.TwoSlopeNorm。
delta = 0.1
x = np.arange(-3.0, 4.001, delta)
y = np.arange(-4.0, 3.001, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (0.9*Z1 - 0.5*Z2) * 2
# select a divergent colormap
cmap = cm.coolwarm
fig, (ax1, ax2) = plt.subplots(ncols=2)
pc = ax1.pcolormesh(Z, cmap=cmap)
fig.colorbar(pc, ax=ax1)
ax1.set_title('Normalize()')
pc = ax2.pcolormesh(Z, norm=colors.CenteredNorm(), cmap=cmap)
fig.colorbar(pc, ax=ax2)
ax2.set_title('CenteredNorm()')
plt.show()

对称对数#
同样地,有时会出现数据既有正数也有负数,但我们仍然希望对两者都应用对数缩放。在这种情况下,负数也会对数缩放,并映射到较小的数值;例如,如果 vmin=-vmax,那么负数从0映射到0.5,正数从0.5映射到1。
由于接近零的值的对数趋向于无穷大,因此需要在零附近的小范围内进行线性映射。参数 linthresh 允许用户指定此范围的大小(-linthresh, linthresh)。此范围在颜色映射中的大小由 linscale 设置。当 linscale == 1.0(默认值)时,用于线性范围正负两半的空间将等于对数范围中的一十年。
N = 100
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
fig, ax = plt.subplots(2, 1)
pcm = ax[0].pcolormesh(X, Y, Z,
norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
vmin=-1.0, vmax=1.0, base=10),
cmap='RdBu_r', shading='auto')
fig.colorbar(pcm, ax=ax[0], extend='both')
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', vmin=-np.max(Z), shading='auto')
fig.colorbar(pcm, ax=ax[1], extend='both')
plt.show()

幂律#
有时,将颜色重新映射到幂律关系上(即 \(y=x^{\gamma}\),其中 \(\gamma\) 是幂)是有用的。为此,我们使用 colors.PowerNorm。它接受一个参数 gamma*(*gamma == 1.0 将仅产生默认的线性归一化):
备注
使用这种类型的变换来绘制数据可能需要一个充分的理由。技术观众习惯于线性和对数轴以及数据变换。幂律较为少见,观众应明确知晓它们已被使用。
N = 100
X, Y = np.mgrid[0:3:complex(0, N), 0:2:complex(0, N)]
Z1 = (1 + np.sin(Y * 10.)) * X**2
fig, ax = plt.subplots(2, 1, layout='constrained')
pcm = ax[0].pcolormesh(X, Y, Z1, norm=colors.PowerNorm(gamma=0.5),
cmap='PuBu_r', shading='auto')
fig.colorbar(pcm, ax=ax[0], extend='max')
ax[0].set_title('PowerNorm()')
pcm = ax[1].pcolormesh(X, Y, Z1, cmap='PuBu_r', shading='auto')
fig.colorbar(pcm, ax=ax[1], extend='max')
ax[1].set_title('Normalize()')
plt.show()

离散边界#
Matplotlib 提供的另一种归一化是 colors.BoundaryNorm。除了 vmin 和 vmax 之外,它还接受作为参数的数据映射之间的边界。颜色在这些“边界”之间线性分布。它还可以接受一个 extend 参数,以在颜色分布的范围上添加超出上限和/或下限的值。例如:
>>> import matplotlib.colors as colors
>>> bounds = np.array([-0.25, -0.125, 0, 0.5, 1])
>>> norm = colors.BoundaryNorm(boundaries=bounds, ncolors=4)
>>> print(norm([-0.2, -0.15, -0.02, 0.3, 0.8, 0.99]))
[0 0 1 2 3 3]
注意:与其他规范不同,此规范返回从0到 ncolors-1 的值。
N = 100
X, Y = np.meshgrid(np.linspace(-3, 3, N), np.linspace(-2, 2, N))
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = ((Z1 - Z2) * 2)[:-1, :-1]
fig, ax = plt.subplots(2, 2, figsize=(8, 6), layout='constrained')
ax = ax.flatten()
# Default norm:
pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r')
fig.colorbar(pcm, ax=ax[0], orientation='vertical')
ax[0].set_title('Default norm')
# Even bounds give a contour-like effect:
bounds = np.linspace(-1.5, 1.5, 7)
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
pcm = ax[1].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r')
fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical')
ax[1].set_title('BoundaryNorm: 7 boundaries')
# Bounds may be unevenly spaced:
bounds = np.array([-0.2, -0.1, 0, 0.5, 1])
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
pcm = ax[2].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r')
fig.colorbar(pcm, ax=ax[2], extend='both', orientation='vertical')
ax[2].set_title('BoundaryNorm: nonuniform')
# With out-of-bounds colors:
bounds = np.linspace(-1.5, 1.5, 7)
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256, extend='both')
pcm = ax[3].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r')
# The colorbar inherits the "extend" argument from BoundaryNorm.
fig.colorbar(pcm, ax=ax[3], orientation='vertical')
ax[3].set_title('BoundaryNorm: extend="both"')
plt.show()

TwoSlopeNorm: 中心两侧的不同映射#
有时我们希望在概念中心点的两侧使用不同的色图,并且我们希望这两个色图具有不同的线性比例。一个例子是地形图,其中陆地和海洋的中心点为零,但陆地通常具有比水域更深的深度范围,并且它们通常由不同的色图表示。
dem = cbook.get_sample_data('topobathy.npz')
topo = dem['topo']
longitude = dem['longitude']
latitude = dem['latitude']
fig, ax = plt.subplots()
# make a colormap that has land and ocean clearly delineated and of the
# same length (256 + 256)
colors_undersea = plt.cm.terrain(np.linspace(0, 0.17, 256))
colors_land = plt.cm.terrain(np.linspace(0.25, 1, 256))
all_colors = np.vstack((colors_undersea, colors_land))
terrain_map = colors.LinearSegmentedColormap.from_list(
'terrain_map', all_colors)
# make the norm: Note the center is offset so that the land has more
# dynamic range:
divnorm = colors.TwoSlopeNorm(vmin=-500., vcenter=0, vmax=4000)
pcm = ax.pcolormesh(longitude, latitude, topo, rasterized=True, norm=divnorm,
cmap=terrain_map, shading='auto')
# Simple geographic plot, set aspect ratio because distance between lines of
# longitude depends on latitude.
ax.set_aspect(1 / np.cos(np.deg2rad(49)))
ax.set_title('TwoSlopeNorm(x)')
cb = fig.colorbar(pcm, shrink=0.6)
cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000])
plt.show()

FuncNorm: 任意函数归一化#
如果上述规范不能提供您所需的归一化,您可以使用 FuncNorm 来定义您自己的归一化。请注意,此示例与 PowerNorm 在幂为 0.5 时相同:
def _forward(x):
return np.sqrt(x)
def _inverse(x):
return x**2
N = 100
X, Y = np.mgrid[0:3:complex(0, N), 0:2:complex(0, N)]
Z1 = (1 + np.sin(Y * 10.)) * X**2
fig, ax = plt.subplots()
norm = colors.FuncNorm((_forward, _inverse), vmin=0, vmax=20)
pcm = ax.pcolormesh(X, Y, Z1, norm=norm, cmap='PuBu_r', shading='auto')
ax.set_title('FuncNorm(x)')
fig.colorbar(pcm, shrink=0.6)
plt.show()

自定义归一化:手动实现两个线性范围#
上述 TwoSlopeNorm 是一个定义你自己规范的有用示例。请注意,为了使颜色条工作,你必须为你的规范定义一个逆函数:
class MidpointNormalize(colors.Normalize):
def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False):
self.vcenter = vcenter
super().__init__(vmin, vmax, clip)
def __call__(self, value, clip=None):
# I'm ignoring masked values and all kinds of edge cases to make a
# simple example...
# Note also that we must extrapolate beyond vmin/vmax
x, y = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1.]
return np.ma.masked_array(np.interp(value, x, y,
left=-np.inf, right=np.inf))
def inverse(self, value):
y, x = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1]
return np.interp(value, x, y, left=-np.inf, right=np.inf)
fig, ax = plt.subplots()
midnorm = MidpointNormalize(vmin=-500., vcenter=0, vmax=4000)
pcm = ax.pcolormesh(longitude, latitude, topo, rasterized=True, norm=midnorm,
cmap=terrain_map, shading='auto')
ax.set_aspect(1 / np.cos(np.deg2rad(49)))
ax.set_title('Custom norm')
cb = fig.colorbar(pcm, shrink=0.6, extend='both')
cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000])
plt.show()

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