首页 > 解决方案 > Matpliblib 颜色图,峰值在中心,边缘为零

问题描述

我正在寻找一个自定义颜色图,它突出显示中心(值 1)并且边缘只有白色(值 0 和 2)。理想情况下,应该有一个从 1 到 [0, 2] 的梯度。

通常的颜色图则相反:偏离中心(中心为白色)。

谢谢你的帮助

标签: matplotlibcolormap

解决方案


您可以从模块中为此使用from_list方法。LinearSegmentedColormapmatplotlib.colors

在这里,我们将 3 种颜色作为列表 ( ["white", "red", "white"])。这可以通过更改任何这些颜色名称轻松自定义。

例如:

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

cmap = LinearSegmentedColormap.from_list('wrw', ["white", "red", "white"], N=256)

a = np.arange(0, 2, 0.01).reshape(20, 10)
fig, ax = plt.subplots()

p = ax.pcolormesh(a, cmap=cmap, vmin=0, vmax=2)

fig.colorbar(p)

plt.show()

在此处输入图像描述


推荐阅读