gpt4 book ai didi

python - 如果 yticks 太多,Seaborn 会自动隐藏 yticks

转载 作者:行者123 更新时间:2023-12-01 02:41:10 30 4
gpt4 key购买 nike

我正在使用seaborn 绘制热图。但如果 yticks 太多,其中一些将被自动隐藏。结果如下:
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()

最佳答案

Seaborn 热图提供参数

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.

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

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

enter image description here

关于python - 如果 yticks 太多,Seaborn 会自动隐藏 yticks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45708277/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com