问题描述
有什么方法可以改变Seaborn热图的像元大小吗?我发现了这但我无法按预期工作.
Is there any way to change cell size of Seaborn heatmap?I found this but I cannot get it work as expected.
因此,我在y轴标签中有很长的文字.由于所有文本均已被砍掉,因此我希望将热图的单元格尺寸缩小得更小.我不需要那个大矩形. (例如,突出显示.)
So, I have long text in y-axis labels. Since all of the texts are chopped off, I would like to shrink cell size of the heatmap much smaller. I don't need that big rectangle. (Highlighted just for example.)
(我隐藏了标签名称.)
(I hid label names.)
当我通过类似方式更改图形尺寸时,
When I change the figure size by something like,
plt.figure(figsize=(8, 6)) or
figure.set_size_inches(12, 12)
单元格也变大了,因此文本仍然被切掉.
the cell gets bigger as well so the texts remain chopped off.
这是代码.
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)
推荐答案
尝试在sns.heatmap
调用中使用square=True
参数.这会将热图单元约束为正方形的长宽比.
Try using the square=True
argument in your sns.heatmap
call. This will constrain the heat map cells to a square aspect ratio.
ax1 = sns.heatmap(hmap, cbar=0, cmap="YlGnBu",linewidths=2, ax=ax0,vmax=3000, vmin=0, square=True)
这篇关于Seaborn热图减少了细胞大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!