【问题标题】:Using imshow() to represent 0 as white使用 imshow() 将 0 表示为白色
【发布时间】:2020-08-16 21:09:42
【问题描述】:

我环顾四周,并没有在网上或 matplotlib 文档中找到解决这个看似简单问题的直接解决方案。我正在使用 imshow() 创建具有发散 cmap(coolwarm)的矩阵的热图,并且我想让 0 表示为白色,将正值表示为红色,将负值表示为蓝色。任何人都知道无需创建自定义 cmap 的简单方法吗?

【问题讨论】:

标签: python matplotlib heatmap imshow


【解决方案1】:

通过使用最小-最大归一化,原始数据中的零被移动,因此如下所示移动零。以下是我认为可以通过这种方式完成的一种方式。


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.

希望这更接近您所需的输出

【讨论】:

    猜你喜欢
    • 2014-04-21
    • 2012-04-24
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多