我有一个数据帧,其结构类似于以下内容:
from datetime import datetime
import pandas as pd
from mpu.datetime import generate # pip install mpu
mind, maxd = datetime(2018, 1, 1), datetime(2018, 12, 30)
df = pd.DataFrame({'datetime': [generate(mind, maxd) for _ in range(10)]})
我想了解这些数据是如何在一天中的几个小时和一周的几天中分布的。我可以通过:
^{pr2}$最后我有了一个情节:
ax = df.groupby(['weekday', 'hour'])['datetime'].count().plot(kind='line', color='blue')
ax.set_ylabel("#")
ax.set_xlabel("time")
plt.show()
这给了我:
但是你可以注意到,很难区分工作日和工作时间,甚至不明显。如何获得类似于以下内容的两级标签?在
如果假设每个可能的工作日和小时都实际出现在数据中,那么轴的单位就是小时,周一午夜为0,周日23h为24*7-1=167。 然后,你可以每24小时用主要的记号勾选一次,并在每一个中午标注一周中的每一天。在
你所说的“熊猫”并不是你的想象。在
您在代码中提供的数据如下所示:
我无法用您的数据集测试它,而pandas datetime有时在matplotlib datetime中很困难。但是我们的想法是分别设置major and minor ticks和{a2}:
样本输出:
相关问题 更多 >
编程相关推荐