Seaborn热力图行为

3

考虑以下代码:

fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, cbar=None, cmap="Blues", linewidths=1, linecolor='black')

fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, cmap="Blues", linewidths=1, linecolor='black')

结果如下:

热图

为什么添加参数cbar=None会改变结果呢?


1
如果所有值都相同(这里是 1),则在确定值到颜色的归一化时存在一个自由参数。使用 vmin/vmax 参数(或 norm 参数)来固定归一化。 - ImportanceOfBeingErnest
3个回答

0
根据 @Alpha 的说明,如 文档 所示,cbar 值应为布尔型。

cbar : 布尔型, 可选参数

是否绘制颜色条。


0

这是cbar的默认行为,某种程度上是一个错误。

关闭cbar:

fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4, 4)), ax=ax, cmap='Blues', linewidths=1, linecolor='black', cbar=False, vmin=0, vmax=2)

enter image description here

打开cbar

fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, cmap='Blues', linewidths=1, linecolor='black', cbar=True, vmin=0, vmax=2)

enter image description here


0

为了让两个图形具有相同的大小,您可以使用:

fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, linewidths=1, linecolor='black', cmap="Blues",)
cbar = ax.collections[0].colorbar
cbar.remove()
plt.show()

enter image description here

和...

fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, linewidths=1, linecolor='black', cmap="Blues",)
cbar = ax.collections[0].colorbar
#cbar.remove()
plt.show()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接