我花了太多时间研究这个问题,一些选项卡仍在我的浏览器中打开: Link1 Link2 Link3 Link4
我应该工作!
无论如何,我的问题是:我使用其他人的脚本来生成大量热图,然后我必须对其进行检查和排序/分配:
下面是一个示例: HM sample
我需要能够轻松区分 0.03 和零,但正如您所看到的,它们看起来几乎相同。理想的解决方案是:白色(仅零)-黄-橙-红或白(仅零)-橙-红
开发人员像这样使用“YlOrRd”:
sns.heatmap(heat_map, annot=True, fmt=".2g", cmap="YlOrRd", linewidths=0.5,
linecolor='black', xticklabels=xticks, yticklabels=yticks
)
我已经尝试了一堆提供的标准/默认颜色图选项,但没有成功。
我没有任何构建彩色 map 的实际经验,并且我不想破坏已经在工作的东西。有人有什么想法吗?
谢谢
**由于它是工作产品,我可以发布的代码/示例受到限制。
最佳答案
一个选项是从现有颜色图中获取颜色,将第一个颜色替换为白色,然后根据这些操纵值创建新的颜色图。
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
import matplotlib.colors
import seaborn as sns
# some data
a = np.array([0.,0.002,.005,.0099,0.01,.0101,.02,.04,.24,.42,.62,0.95,.999,1.])
data = np.random.choice(a, size=(12,12))
# create colormap. We take 101 values equally spaced between 0 and 1
# hence the first value 0, second value 0.01
c = np.linspace(0,1,101)
# For those values we store the colors from the "YlOrRd" map in an array
colors = plt.get_cmap("YlOrRd",101)(c)
# We replace the first row of that array, by white
colors[0,:] = np.array([1,1,1,1])
# We create a new colormap with the colors
cmap = matplotlib.colors.ListedColormap(colors)
# Plot the heatmap. The format is set to 4 decimal places
# to be able to disingush specifically the values ,.0099, .0100, .0101,
sns.heatmap(data, annot=True, fmt=".4f", cmap=cmap, vmin=0, vmax=1,
linewidths=0.5, linecolor='black')
plt.show()
关于Python:在 Seaborn 中更改/自定义颜色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52781660/