1

我有完美組織時間戳數據幀,如下圖所示:如何分組熊貓時間戳在一個圖中繪製幾個圖,並將它們疊加在matplotlib中?

enter image description here

這是一個網絡日誌和時間戳去,雖然全年。我想將它們切割成每一天,並在每個小時內顯示訪問並將它們繪製成相同的圖形並將它們堆疊在一起。就像下圖所示的圖:

enter image description here

我削減他們進入天做得很好,分別繪製了一天的訪問,但我有麻煩他們繪製和堆疊在一起。我正在使用的主要工具是Pandas和Matplotlib

任何建議和意見?非常感激!


編輯:

我的代碼如下:

的時間戳:https://gist.github.com/adamleo/04e4147cc6614820466f7bc05e088ac5

而且數據幀是這樣的: enter image description here

我繪製的時間戳密度貫穿整個使用週期E碼如下:

timestamps_series_all = pd.DatetimeIndex(pd.Series(unique_visitors_df.time_stamp)) 
timestamps_series_all_toBePlotted = pd.Series(1, index=timestamps_series_all) 
timestamps_series_all_toBePlotted.resample('D').sum().plot() 

,並得到了結果:

timestamps_series_oneDay = pd.DatetimeIndex(pd.Series(unique_visitors_df.time_stamp.loc[unique_visitors_df["date"] == "2014-08-01"])) 
timestamps_series_oneDay_toBePlotted = pd.Series(1, index=timestamps_series_oneDay) 
timestamps_series_oneDay_toBePlotted.resample('H').sum().plot() 

和結果: enter image description here

enter image description here

我使用的代碼繪製在一天之內的時間戳

現在我是灰泥ķ。

我真的很感謝您的幫助!

+0

你有沒有考慮分享一些你的代碼? –

+0

@StephenRauch嗨,哥們,我剛更新了帖子。請看一下。謝謝。 –

回答

2

我想你需要pivot

#https://gist.github.com/adamleo/04e4147cc6614820466f7bc05e088ac5 to L 
df = pd.DataFrame({'date':L}) 
print (df.head()) 
        date 
0 2014-08-01 00:05:46 
1 2014-08-01 00:14:47 
2 2014-08-01 00:16:05 
3 2014-08-01 00:20:46 
4 2014-08-01 00:23:22 

#convert to datetime if necessary 
df['date'] = pd.to_datetime(df['date']) 
#resample by Hours, get count and create df 
df = df.resample('H', on='date').size().to_frame('count') 
#extract date and hour 
df['days'] = df.index.date 
df['hours'] = df.index.hour 
#pivot and plot 
#maybe check parameter kind='density' from http://stackoverflow.com/a/33474410/2901002 
#df.pivot(index='days', columns='hours', values='count').plot(rot='90') 
#edit: last line change to below: 
df.pivot(index='hours', columns='days', values='count').plot(rot='90') 
+1

非常感謝你的好友,這個答案真的有幫助。然而,最後一行有點扭曲,所以我改爲'df.pivot(index ='hours',columns ='days',values ='count')。plot(rot = '90')' 。不勝感激! –

相關問題