简体   繁体   English

时间序列Python中每小时数据的箱形图

[英]Box plot of hourly data in Time Series Python

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. 下面给出了示例输出。

样本预期输出图像

IIUC, you need, which gives you a box of speed during each hour of the a day: 您需要IIUC,它在一天的每个小时内为您提供一箱速度:

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

Output: 输出:

在此处输入图片说明

You can also use seaborn: 您还可以使用seaborn:

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

output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM