Seaborn自动隐藏yticks如果有太多的yticks

2024-06-06 01:04:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用seaborn绘制热图。但是如果有太多的ytick,其中一些会自动隐藏起来。结果如下:
heatmap

如您所见,yticks只显示1、3、5、7。。。。31、33岁 我怎样才能让seaborn或matplotlib显示所有这些,比如:1、2、3、4……31、32、33、34?在

我的代码是:

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

y = np.random.randint(1, 100, 510)
y = y.reshape((34,15))
df = pd.DataFrame(y, columns=[x for x in 'wwwwwwwwwwwwwww'], index=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34'])
sns.heatmap(df, annot=True)
plt.yticks(rotation=0)
plt.show()

Tags: 代码importpandasdfmatplotlibasnp绘制
1条回答
网友
1楼 · 发布于 2024-06-06 01:04:08

Seabornheatmap提供参数

xticklabels, yticklabels : “auto”, bool, list-like, or int, optional
If True, plot the column names of the dataframe. If False, don’t plot the column names. If list-like, plot these alternate labels as the xticklabels. If an integer, use the column names but plot only every n label. If “auto”, try to densely plot non-overlapping labels.

因此,easies的解决方案是添加yticklabels=1作为参数。在

sns.heatmap(df, annot=True, yticklabels=1)

enter image description here

相关问题 更多 >