确保 0 在 RdBu 颜色条中变为白色 [英] Making sure 0 gets white in a RdBu colorbar

查看:31
本文介绍了确保 0 在 RdBu 颜色条中变为白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码段创建了一个热图:

 将numpy导入为np导入matplotlib.pyplot作为pltd = np.random.normal(.4,2,(10,10))plt.imshow(d,cmap=plt.cm.RdBu)plt.colorbar()plt.show()

结果如下图所示:

现在,由于数据的中点不为0,因此颜色表中值为0的单元格不是白色,而是略带红色.

我如何强制颜色图使max = blue,min = red和0 = white?

解决方案

使用

I create a heatmap with the following snippet:

import numpy as np
import matplotlib.pyplot as plt
d = np.random.normal(.4,2,(10,10))
plt.imshow(d,cmap=plt.cm.RdBu)
plt.colorbar()
plt.show()

The result is plot below:

Now, since the middle point of the data is not 0, the cells in which the colormap has value 0 are not white, but rather a little reddish.

How do I force the colormap so that max=blue, min=red and 0=white?

解决方案

Use a DivergingNorm.

Note: From matplotlib 3.2 onwards DivergingNorm is renamed to TwoSlopeNorm

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

d = np.random.normal(.4,2,(10,10))

norm = mcolors.DivergingNorm(vmin=d.min(), vmax = d.max(), vcenter=0)
plt.imshow(d, cmap=plt.cm.RdBu, norm=norm)

plt.colorbar()
plt.show()

这篇关于确保 0 在 RdBu 颜色条中变为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆