简体   繁体   English

Seaborn 热图裁剪 y 轴刻度标签

[英]Seaborn heatmap clipping y-axis tick labels

I'm attempting to create a heatmap with a DataFrame with rows like:我正在尝试使用 DataFrame 创建一个热图,其行如下:

у     7.14e-02  4.29e-01  5.00e-01
ф     0.00e+00  1.00e+00  0.00e+00
х     0.00e+00  1.00e+00  0.00e+00
ц     0.00e+00  1.00e+00  0.00e+00
ч     0.00e+00  9.75e-01  2.50e-02
ш     0.00e+00  1.00e+00  0.00e+00
щ     0.00e+00  1.00e+00  0.00e+00

However, when I try to create a heatmap from this data, I end up with a y-axis that looks like this (note letters like у, ф, and ш with their bottom/right clipped off).然而,当我尝试从这些数据创建热图时,我最终得到一个看起来像这样的 y 轴(注意像 у、ф 和 ш 这样的字母,它们的底部/右侧被剪掉了)。

y轴

Here's the code I'm using to initialize it:这是我用来初始化它的代码:

fig, ax = plt.subplots(figsize=(15, 15)
plot = sns.heatmap(df_result, annot=True, fmt=".2f")

EDIT: I have tested replacing the Cyrillic character 'у' with the Latin character 'y' and it is no longer clipped.编辑:我已经测试用拉丁字符'y'替换西里尔字符'у'并且它不再被剪裁。 Any reason this issue would be specific to Cyrillic characters?这个问题有什么理由特定于西里尔字符吗?

The issue may be in the encoding when reading in the csv.读取 csv 时,问题可能出在编码中。

Here is the csv:这是 csv:

name,value1,value2,value3
у, 7.14e-02, 4.29e-01, 5.00e-01
ф, 0.00e+00, 1.00e+00, 0.00e+00
х, 0.00e+00, 1.00e+00, 0.00e+00
ц, 0.00e+00, 1.00e+00, 0.00e+00
ч, 0.00e+00, 9.75e-01, 2.50e-02
ш, 0.00e+00, 1.00e+00, 0.00e+00
щ, 0.00e+00, 1.00e+00, 0.00e+00

Read in the csv as:读入 csv 为:

df = pd.read_csv("data.csv",encoding='utf-8')

Save names list as y_labels and graph:将名称列表另存为 y_labels 和图形:

y_labels = df['name'].tolist()

fig, ax = plt.subplots(figsize = (15, 15))
ax  = sns.heatmap(df[['value1','value2','value3']],
yticklabels =  y_labels)

y-axis labels are not cut off. y 轴标签不会被截断。 在此处输入图像描述

Rotating the yticks would help with the clipping.旋转 yticks 将有助于剪辑。

degrees = 90
plt.yticks(rotation=degrees)

Or you could add some padding for the yaxis.或者您可以为 yaxis 添加一些填充。

n = 15
ax.tick_params(axis='y', which='major', pad=n)

Documentation for padding with matplotlib can be found here .可以在此处找到使用 matplotlib 进行填充的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM