Seaborn热图减少了细胞大小

时间:2017-05-31 04:34:15

标签: python-3.x matplotlib heatmap seaborn

有没有办法改变Seaborn热图的细胞大小? 我发现this但我无法按预期工作。

所以,我在y轴标签上有很长的文字。由于所有文本都被删除了,我想将热图的单元尺寸缩小得更小。我不需要那个大矩形。 (仅举例说明。)

enter image description here (我隐藏了标签名称。)

当我用类似的方式改变数字大小时,

plt.figure(figsize=(8, 6)) or 
figure.set_size_inches(12, 12) 

细胞变得更大,所以文本仍然被切断。

这是代码。

sns.set(font_scale=1.2)
ax0 = plt.axes()
ax1 = sns.heatmap(hmap, cbar=0, cmap="YlGnBu",linewidths=2, ax=ax0,vmax=3000, vmin=0)
ax1.set_title('test heatmap')

for item in ax1.get_yticklabels():
    item.set_rotation(0)

for item in ax1.get_xticklabels():
    item.set_rotation(0)

figure = plt.gcf()  # get current figure
figure.set_size_inches(12, 12)
plt.savefig('test.png') , dpi=400)

2 个答案:

答案 0 :(得分:3)

您实际上并不想更改单元格大小,而是希望缩小轴的大小。如何做到这一点:

  • 使用plt.tight_layout()
  • 为侧面提供更多空间,例如通过fig.subplots_adjust(left=0.4)
  • 创建一个具有所需大小的轴ax1 = fig.add_axes([0.4,0.2,0.5,0.6])(数字为[left, bottom, width, height],并使用此轴绘制热图,sns.heatmap(... , ax=ax1)

答案 1 :(得分:3)

尝试在square=True来电中使用sns.heatmap参数。这会将热图单元格约束为方形纵横比。

ax1 = sns.heatmap(hmap, cbar=0, cmap="YlGnBu",linewidths=2, ax=ax0,vmax=3000, vmin=0, square=True)
相关问题