【问题标题】:Box plot of hourly data in Time Series Python时间序列 Python 中每小时数据的箱线图
【发布时间】:2019-11-15 12:09:29
【问题描述】:

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

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

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

【问题讨论】:

标签: python pandas matplotlib time-series seaborn


【解决方案1】:

你也可以使用 seaborn:

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

输出:

【讨论】:

  • 谢谢,你能不能帮忙把相框做得更大一点。我有一个非常小的帧大小图
  • 执行fig, ax = plt.subplots(figsize=(12,8)),然后将ax=ax 传递给sns.boxplot,或在斯科特的回答中传递给plot.box()
  • 想更新问题,但是我单独问了。如何为每一天的每一小时绘制一个箱线图? stackoverflow.com/questions/56893658/…
【解决方案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()

输出:

【讨论】:

  • 谢谢,你能不能帮忙把相框做得更大一点。我有一个非常小的帧大小图
  • 将 figsize=(10,8) 作为参数添加到 box 方法中。
猜你喜欢
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多