【发布时间】:2021-12-18 03:51:49
【问题描述】:
目标:我想从具有 2 个索引的数据框创建热图。第二个索引,称为“Profile_Name”,也用于循环遍历数据框以创建多个热图。
问题:sns.heatmap 不允许我更改 y 轴上的值。
重新创建数据框的代码
df = pd.DataFrame([['Monday', 1, 'Sebastian', 3],
['Monday', 2, 'Sebastian', 6],
['Monday', 3, 'Living room', 10],
['Tuesday', 1,'Sebastian', 6],
['Tuesday', 2,'Sebastian', 3],
['Tuesday', 3,'Sebastian', 8],
['Wednesday', 1,'Sebastian', 6],
['Wednesday', 2,'Sebastian', 3],
['Wednesday', 3,'Sebastian', 9],
['Tuesday', 1,'Living room', 6],
['Monday', 2,'Living room', 10]],
columns=['Weekday', 'Hour', 'Profile_Name', 'Counts'])
heatmap_df = df.pivot_table(index=('Hour', 'Profile_Name') ,columns='Weekday',values='Counts', aggfunc=lambda x:x).fillna(0)
heatmap_df.head()
创建热图的代码
Names = df.Profile_Name.unique()
for Profile in Names:
plt.subplots(figsize=(15,10))
sns.heatmap(heatmap_df[heatmap_df.index.get_level_values('Profile_Name').isin([Profile])], annot= True, fmt='g', cmap="Blues")
plt.title(Profile, fontsize = 20) # title with fontsize 20
plt.xlabel('Weekday', fontsize = 15) # x-axis label with fontsize 15
plt.ylabel('Time', fontsize = 15) # y-axis label with fontsize 15
#ax.set_yticks(range(0,24)) ## doesnt work
plt.show()
如何从 y 轴删除第二个索引“Profile_Name”,在屏幕截图中显示为“Sebastian”? 我希望 y 轴仅显示“小时”索引或完全覆盖它并在 y 轴上设置值 0-23。
【问题讨论】:
-
感谢@TrentonMcKinney 的建议 :) 希望我对问题的新编辑做得正确
标签: python matplotlib seaborn heatmap yaxis