Box plot of hourly data in Time Series Python

2019-08-17 10:16发布

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()

Data sample

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.

Sample Expected output image

2条回答
做自己的国王
2楼-- · 2019-08-17 10:34

You can also use seaborn:

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

output:

enter image description here

查看更多
男人必须洒脱
3楼-- · 2019-08-17 10:48

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:

enter image description here

查看更多
登录 后发表回答