色图归一化 SymLogNorm#

演示如何使用 norm 以非线性的方式将色图映射到数据上。

合成数据集由两个驼峰组成,一个负值,一个正值,其中正值驼峰的振幅是负值驼峰的8倍。在线性尺度下,负值驼峰几乎不可见,并且很难看清其轮廓的任何细节。当对正值和负值应用对数缩放时,更容易看到每个驼峰的形状。

参见 SymLogNorm

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.colors as colors


def rbf(x, y):
    return 1.0 / (1 + 5 * ((x ** 2) + (y ** 2)))

N = 200
gain = 8
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
Z1 = rbf(X + 0.5, Y + 0.5)
Z2 = rbf(X - 0.5, Y - 0.5)
Z = gain * Z1 - Z2

shadeopts = {'cmap': 'PRGn', 'shading': 'gouraud'}
colormap = 'PRGn'
lnrwidth = 0.5

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

pcm = ax[0].pcolormesh(X, Y, Z,
                       norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
                                              vmin=-gain, vmax=gain, base=10),
                       **shadeopts)
fig.colorbar(pcm, ax=ax[0], extend='both')
ax[0].text(-2.5, 1.5, 'symlog')

pcm = ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain,
                       **shadeopts)
fig.colorbar(pcm, ax=ax[1], extend='both')
ax[1].text(-2.5, 1.5, 'linear')
colormap normalizations symlognorm

为了找到任何特定数据集的最佳可视化效果,可能需要尝试多种不同的颜色尺度。除了 SymLogNorm 缩放,还可以选择使用 AsinhNorm(实验性功能),它在应用于数据值“Z”的线性变换区域和对数变换区域之间具有更平滑的过渡。在下面的图中,尽管数据集中本身没有尖锐特征,但在每个驼峰周围仍可能看到等高线状的伪影。asinh 缩放显示每个驼峰更平滑的着色效果。

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

pcm = ax[0].pcolormesh(X, Y, Z,
                       norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
                                              vmin=-gain, vmax=gain, base=10),
                       **shadeopts)
fig.colorbar(pcm, ax=ax[0], extend='both')
ax[0].text(-2.5, 1.5, 'symlog')

pcm = ax[1].pcolormesh(X, Y, Z,
                       norm=colors.AsinhNorm(linear_width=lnrwidth,
                                             vmin=-gain, vmax=gain),
                       **shadeopts)
fig.colorbar(pcm, ax=ax[1], extend='both')
ax[1].text(-2.5, 1.5, 'asinh')


plt.show()
colormap normalizations symlognorm

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

由 Sphinx-Gallery 生成的画廊