我在网上和matplotlib文档中寻找了很久,但似乎没有一个简单直接的解决方案来解决这个看似简单的问题。我正在使用imshow()函数创建一个矩阵的热力图,并且使用diverging cmap(coolwarm),我希望将0表示为白色,正值表示为红色,负值表示为蓝色。有没有人知道一种不需要创建自定义cmap的简便方法?
我在网上和matplotlib文档中寻找了很久,但似乎没有一个简单直接的解决方案来解决这个看似简单的问题。我正在使用imshow()函数创建一个矩阵的热力图,并且使用diverging cmap(coolwarm),我希望将0表示为白色,正值表示为红色,负值表示为蓝色。有没有人知道一种不需要创建自定义cmap的简便方法?
通过使用最小-最大归一化,原始数据中的零被移动,因此请按照下面所示的方式将零进行移动。以下是我想出的一种方法。
data = np.array([[0.000000,5.67],[-0.231049,0.45],[-0.231049,0.000000]])
k=(data-np.min(data))/(np.max(data)-np.min(data)) # Min-max Normalization
nsv_zero =-np.min(data)/(np.max(data)-np.min(data)) # new shifted value of zero
sns.heatmap(np.where( k == nsv_zero ,0.5 ,k),vmin=0,vmax=1,cmap='coolwarm',annot=data)
* Min-Max Normalization : Scale all the values to new range(0,1)
* Shifting zero in initial data to 0.5 in the new output data so as to get white color
* Here I am using modified data on heatmap, but I am using the original annotations only.