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')' 。不胜感激! –

相关问题