每月每一天的子图,按月分组,X 轴按小时分组

问题描述

我需要创建一个 6x5 的子图,其中每个图代表一个月中的一天 (1-30) 并按月分组。

下面的示例数据有一些虚拟变量“September-December”,如果是那个月,则为 1(在分组时可能会有所帮助)。每行代表一天中那个小时的销售额。

样本数据:

enter image description here

这是 1 个图的示例,然而这已将所有数据聚合为 1 个图,我想按月中的某天将其分解,因此应该有 30 个图,其中 X -Axis 是一天中的小时 (1-24),图例包含 9-12 个月(基本上按月份分组),并且这些值是计数变量的聚合(总和)。

fig,ax = plt.subplots()
ax.set_xticks(df['hour'].unique())
df.groupby(["hour","month"]).sum()['count'].unstack().plot(ax=ax)

enter image description here

我发现一些可能有用的代码是这样的:

fig = plt.figure(figsize=(20,20))
ax.set_ylabel('aggregated (sum) of count variable')
for i in range(1,31):
    df_daily = df[df['day'] == i] # select dataframe with day = i
    ax = fig.add_subplot(30,1,i) # add subplot in the i-th position on a grid 30x1   
    ax.plot(df_daily['day'],df_daily['count'])
    ax.set_xticks(df_daily['hour'].unique()) # set x axis 

但它输出一个奇怪的子图:

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)