2013-03-18 51 views
6

我有一個向量在Python中的日期(日期時間)。我怎樣才能繪製一個直方圖15分鐘從這個矢量的事件?從日期時間向量的時間倉的直方圖

這裏是我做過什麼:

StartTime = [] 
for b in myEvents: 
    StartTime.append(b['logDate'].time()) 

正如你看到的,我轉換的日期,時間。 (我從MongoDB中查詢得到myEvents)

fig2 = plt.figure() 
ax = fig2.add_subplot(111) 
ax.grid(True,which='both') 
ax.hist(StartTime,100) 

我得到的錯誤是:

TypeError: can't compare datetime.time to float 

我理解錯誤,但我無法弄清楚如何解決它。

非常感謝您的幫助

+0

你必須將'時間'轉換爲'float',因爲這是直方圖函數唯一可以理解的。您可能會發現[此代碼](http://ubuntuforums.org/showthread.php?t=700216)有幫助。 – Floris 2013-03-18 13:42:59

+0

可能的重複:http://stackoverflow.com/questions/8369584/plot-histogram-of-datetime-time-python-matplotlib – askewchan 2013-03-18 13:43:17

+1

@askewchan我已經讀過這個問題,但無法使用它 – otmezger 2013-03-18 13:45:28

回答

3

如果你想斌的小時,分​​鍾或秒,它應該是很容易的:

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 

此外,十五分鐘垃圾箱,但問題在這裏現在的單位是十進制小時:

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