gpt4 book ai didi

python - 如何更改 matplotlib 中颜色图的强度?

转载 作者:行者123 更新时间:2023-11-28 20:39:39 25 4
gpt4 key购买 nike

我使用 matplotlib.pyplot.pcolor()使用 matplotlib 绘制热图:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1)
data = np.sort(np.random.rand(8,12))
plt.figure()
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap='RdBu', vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.show()

enter image description here

如何更改 'RdBu' 颜色图的强度?例如,如果颜色是 (0, 0, 1),则应将其转换为 (0, 0, 0.8)。更普遍,如果颜色是(x, y, z),应该转化为(ax, ay, az),其中a是一些介于零和一之间的标量。

最佳答案

这与 Stanley R(编辑:现在是 Serenity)的回答非常相似,没有(在我看来)不必要的循环复杂性,附加到列表等:

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

a = 0.5

# Get the colormap colors, multiply them with the factor "a", and create new colormap
my_cmap = plt.cm.RdBu(np.arange(plt.cm.RdBu.N))
my_cmap[:,0:3] *= a
my_cmap = ListedColormap(my_cmap)

np.random.seed(1)
data = np.sort(np.random.rand(8,12))
plt.figure()
plt.subplot(121)
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap='RdBu', vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.subplot(122)
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap=my_cmap, vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.show()

enter image description here

关于python - 如何更改 matplotlib 中颜色图的强度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37517587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com