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

时间:2019-07-04 19:04:54

标签: python pandas matplotlib time-series seaborn

如何按给定的频率分组,例如说每小时,并为时间序列数据集中的一列创建一组箱形图?

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

如何按给定频率分组,例如说每小时,并创建一组速度箱图?下面给出了示例输出。

Sample Expected output image

2 个答案:

答案 0 :(得分:2)

IIUC,您需要,它在一天的每个小时内为您提供一箱速度:

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

输出:

enter image description here

答案 1 :(得分:1)

您也可以使用seaborn:

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

输出:

enter image description here