gpt4 book ai didi

python - 自定义颜色图

转载 作者:行者123 更新时间:2023-12-03 20:29:31 25 4
gpt4 key购买 nike

我想用 绘制热图自定义颜色图 类似于这个,虽然不完全相同。

enter image description here

我想要一个像这样的颜色图。在区间 [-0.6, 0.6] 中,颜色为浅灰色。高于 0.6,红色变深。低于 -0.6 的另一种颜色,比如蓝色,会增强。

如何使用 python 和 matplotlib 创建这样的颜色图?

到目前为止我所拥有的:
seaborn有命令 seaborn.diverging_palette(220, 10, as_cmap=True)它产生一个从蓝光灰到红的颜色图。但是仍然与 [-0.6, 0.6] 没有差距。

enter image description here

最佳答案

颜色图在 0..1 范围内标准化。因此,如果您的数据限制为 -1..1,则 -0.6 将标准化为 0.2,+0.6 将标准化为 0.8。

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

norm = matplotlib.colors.Normalize(-1,1)
colors = [[norm(-1.0), "darkblue"],
[norm(-0.6), "lightgrey"],
[norm( 0.6), "lightgrey"],
[norm( 1.0), "red"]]

cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)


fig, ax=plt.subplots()
x = np.arange(10)
y = np.linspace(-1,1,10)
sc = ax.scatter(x,y, c=y, norm=norm, cmap=cmap)
fig.colorbar(sc, orientation="horizontal")
plt.show()

enter image description here

关于python - 自定义颜色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52626103/

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