matplotlib - Matpliblib 颜色图,峰值在中心,边缘为零
问题描述
我正在寻找一个自定义颜色图,它突出显示中心(值 1)并且边缘只有白色(值 0 和 2)。理想情况下,应该有一个从 1 到 [0, 2] 的梯度。
通常的颜色图则相反:偏离中心(中心为白色)。
谢谢你的帮助
解决方案
您可以从模块中为此使用from_list
方法。LinearSegmentedColormap
matplotlib.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()
推荐阅读
- angular - html上的角度绑定数据与来自后端的数据带有点('。')
- oracle - 如何使用来自普通游标的值打开 sys_refcursor?
- php - 如何在单个模板中打印或回显分类的 ACF 转发器字段?
- ruby - Gem::Gem Not Found 异常
- html - 如何为响应式 img 预留空间(阻止块向下跳)
- python - numpy genfromtxt - 如果未提供标题,则推断列标题
- .net-core - 如何让 HttpClient GetAsync 等待异步加载数据的网页?
- python - 更快的屏幕捕获
- javascript - React:如何在 React 更新表单中显示已经存在的图像,然后在上传新图像后显示新图像?
- c - 将整数值作为二进制分配给C中的变量