Box plot of hourly data in Time Series Python

跟風遠走 提交于 2019-12-13 03:36:57

问题


How to group by a given frequency let say Hourly, and create a set of box plot for one column in a time series data set ?

range = pd.date_range('2015-01-01', '2015-12-31', freq='1min')
df = pd.DataFrame(index = range)

# Average speed in miles per hour
df['speed'] = np.random.randint(low=0, high=60, size=len(df.index))
# Distance in miles (speed * 0.5 hours)
df['distance'] = df['speed'] * 0.25 
# Cumulative distance travelled
df['cumulative_distance'] = df.distance.cumsum()
df.head()

How to group by a given frequency let say Hourly, and create a set of box plot for speed ? A sample output is given below.


回答1:


IIUC, you need, which gives you a box of speed during each hour of the a day:

#You need to reshape your dataframe with hours as column headers
df.set_index(df.index.hour, append=True)['speed'].unstack().plot.box()

Output:




回答2:


You can also use seaborn:

sns.boxplot(x=df.index.hour, y=df.speed)

output:



来源:https://stackoverflow.com/questions/56893208/box-plot-of-hourly-data-in-time-series-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!