yticklabels重叠:是否有可能控制seaborn的热图细胞大小? [英] Overlapping yticklabels: Is it possible to control cell size of heatmap in seaborn?

查看:94
本文介绍了yticklabels重叠:是否有可能控制seaborn的热图细胞大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约200个观测值的数据集,我想将其绘制为热图.每个观察都有一个与之关联的字符串,我想显示它.我的问题是我无法阅读这些标签,因为它们相互重叠.因此,我的问题是,是否可以将热图的单元格大小设置为yticklabel的字体大小,或者是否有其他解决方法.

I have a dataset with around 200 observations which I would like to plot as a heatmap. Each observation has a string associated with it which I would like to display. My problem is that I cannot read these labels since they overlap each other. My question is therefore, whether one can somehow set the cell size of the heatmap to the font size of the yticklabel or whether there is any other workaround for that.

在下面的示例中,我出于说明目的使用随机数据:

In my example below, I use random data for illustration purposes:

import seaborn as sns
import numpy as np
data = np.random.rand(200, 10)
ax = sns.heatmap(data)
for item in ax.get_yticklabels():
    item.set_rotation(0)

这给了我

是否有办法使这些yticklabels具有可读性?在理想情况下,我可以使用一个选项,将单元格的高度设置为yticklabels的字体大小.有可能吗?

Is there a way to make these yticklabels readable? In the ideal case, I would have an option that allows me to set the cells' height to the fontsize of the yticklabels. Is that possible?

如评论中所述,一种可能性是增加图形的大小.我尝试如下:

As mentioned in the comments, one possibility would be to increase the figure's size. I tried that as follows:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(200, 10)

fig, ax = plt.subplots()
fig.set_size_inches(38.5, 10.5)

ax2 = sns.heatmap(data, ax=ax)
for item in ax2.get_yticklabels():
    item.set_rotation(0)

这给了我相同的输出.我使用正确吗?

This gives me the same output. Am I using it correctly?

推荐答案

为标签腾出更多空间的唯一方法是增加矩阵的高度.唯一的其他选择是减小字体大小,但是我想那不是您想要的. 因此,您可以根据矩阵中的行数和标签的字体大小来计算理想的图形高度.保存结果图后,您将获得预期的结果.似乎您在调用plt.show()时看到的GUI窗口的高度被限制为屏幕高度:

The only way to make more room for the labels is to increase the height of the matrix. The only other option would be to decrease the font size, but I guess that's not what you want. So you can compute the ideal figure height based on the number of rows in the matrix and the font size of the labels. When you save the resulting plot you get the expected result. It seems that the height of the GUI window which you see when calling plt.show() is limited to the screen height:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# create some random data
data = np.random.rand(150, 10)

# get the tick label font size
fontsize_pt = plt.rcParams['ytick.labelsize']
dpi = 72.27

# comput the matrix height in points and inches
matrix_height_pt = fontsize_pt * data.shape[0]
matrix_height_in = matrix_height_pt / dpi

# compute the required figure height 
top_margin = 0.04  # in percentage of the figure height
bottom_margin = 0.04 # in percentage of the figure height
figure_height = matrix_height_in / (1 - top_margin - bottom_margin)


# build the figure instance with the desired height
fig, ax = plt.subplots(
        figsize=(6,figure_height), 
        gridspec_kw=dict(top=1-top_margin, bottom=bottom_margin))

# let seaborn do it's thing
ax = sns.heatmap(data, ax=ax)

# save the figure
plt.savefig('/tmp/test.png')

结果:

这篇关于yticklabels重叠:是否有可能控制seaborn的热图细胞大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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