直方图与日期时间向量的时间箱

otm*_*ger 6 python datetime matplotlib histogram

我在python中有一个带日期(日期时间)的向量.如何根据此向量上的出现情况绘制15分钟的柱状图?

这是我做的:

StartTime = []
for b in myEvents:
    StartTime.append(b['logDate'].time())
Run Code Online (Sandbox Code Playgroud)

如您所见,我将日期转换为时间.(我从mongoDB查询中获取myEvents)

fig2 = plt.figure()
ax = fig2.add_subplot(111)
ax.grid(True,which='both')
ax.hist(StartTime,100)
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

TypeError: can't compare datetime.time to float
Run Code Online (Sandbox Code Playgroud)

我理解错误,但我无法弄清楚如何解决它.

非常感谢您的帮助

ask*_*han 6

如果你想按小时,分钟或秒进行分区,那应该很容易:

ts = np.array([ datetime.time(random.randint(24),*random.randint(60,size=2)) for i in range(100) ])
hist([t.hour for t in ts], bins = 24) # to bin by hour
Run Code Online (Sandbox Code Playgroud)

此外,十五分钟的箱子,但现在的问题是单位是十进制小时:

hist([t.hour + t.minute/60. for t in ts], bins = 24*60/15)
Run Code Online (Sandbox Code Playgroud)