如何在热图中设置中心颜色 [英] How to set center color in heatmap
问题描述
我想在 seaborn 中绘制热图.我的代码如下:
plt.rcParams['font.size'] = 13plt.rcParams['font.weight'] = '粗体'my_dpi=96图, ax = plt.subplots(figsize=(800/my_dpi, 600/my_dpi), dpi=my_dpi, facecolor='black')rdgn = sns.diverging_palette(h_neg=130, h_pos=10, s=99, l=55, sep=3)sns.heatmap(df, cmap=rdgn, center=0.00, annot=True, fmt='.2%', linewidths=1.3, linecolor='black', cbar=False, ax=ax)plt.savefig('./image/image.png', dpi=96, facecolor='black')
结果如下:
如果不是将颜色图居中,而是要移动其中间点,则不能使用 center
.而是一个 matplotlib.colors.DivergingNorm
.
将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt从 matplotlib.colors 导入 DivergingNorm将 seaborn 作为 sns 导入数据 = np.linspace(-0.34, 1.31, 100).reshape(10,10)图, ax = plt.subplots()rdgn = sns.diverging_palette(h_neg=130, h_pos=10, s=99, l=55, sep=3, as_cmap=True)divnorm = DivergingNorm(vmin=data.min(), vcenter=0, vmax=data.max())sns.heatmap(data, cmap=rdgn, norm=divnorm, annot=True, fmt='.0%',linewidths=1.3, linecolor='black', cbar=True, ax=ax)plt.show()
这里,完整的颜色图将在绿色部分被挤压并在红色部分被拉伸.
I want to plot a heatmap in seaborn. My code is following:
plt.rcParams['font.size'] = 13
plt.rcParams['font.weight'] = 'bold'
my_dpi=96
fig, ax = plt.subplots(figsize=(800/my_dpi, 600/my_dpi), dpi=my_dpi, facecolor='black')
rdgn = sns.diverging_palette(h_neg=130, h_pos=10, s=99, l=55, sep=3)
sns.heatmap(df, cmap=rdgn, center=0.00, annot=True, fmt ='.2%', linewidths=1.3, linecolor='black', cbar=False, ax=ax)
plt.savefig('./image/image.png', dpi=96, facecolor='black')
And the result is following: enter image description here
I want the set 0 to be white, and the value >0 to be red, the values which <0 to be green. But the center in heatmap is invalid.
By the way, how to set the color unsymmetrical. Because the min value in my data is -0.34 and the maxima is 1.31. I want to set 0 to be white, -0.34 to be greenest and 1.31 to be reddest.
center
would require something that can be centered. So instead of a palette, which is a list of colors, you will need a colormap. Seaborn provides the as_cmap
parameter for this case,
sns.diverging_palette(..., as_cmap=True)
Alternatively, you can of course use any other matplotlib colormap, or specify your custom colormap.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
data = np.linspace(-0.34, 1.31, 100).reshape(10,10)
fig, ax = plt.subplots()
rdgn = sns.diverging_palette(h_neg=130, h_pos=10, s=99, l=55, sep=3, as_cmap=True)
sns.heatmap(data, cmap=rdgn, center=0.00, annot=True, fmt ='.0%',
linewidths=1.3, linecolor='black', cbar=True, ax=ax)
plt.show()
If instead of centering the colormap you want to shift its middle point you cannot use center
. But instead a matplotlib.colors.DivergingNorm
.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import DivergingNorm
import seaborn as sns
data = np.linspace(-0.34, 1.31, 100).reshape(10,10)
fig, ax = plt.subplots()
rdgn = sns.diverging_palette(h_neg=130, h_pos=10, s=99, l=55, sep=3, as_cmap=True)
divnorm = DivergingNorm(vmin=data.min(), vcenter=0, vmax=data.max())
sns.heatmap(data, cmap=rdgn, norm=divnorm, annot=True, fmt ='.0%',
linewidths=1.3, linecolor='black', cbar=True, ax=ax)
plt.show()
Here, the full colormap will be squeezed in the green part and stretched in the red part.
这篇关于如何在热图中设置中心颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!