【问题标题】:Plot time series matplotlib with lots of data points绘制具有大量数据点的时间序列 matplotlib
【发布时间】:2017-02-11 13:00:38
【问题描述】:

我想为包含 12 个月数据的数据集绘制时间序列。但是,数据会在 12 个月内每天每小时记录一次。整个数据集有超过 8000 个数据点。数据格式如下

        Date   Time  Energy
0 2014-01-01   1     1118.1
1 2014-01-01   2     1233.2
2 2014-01-01   3     1278.2
.     .        .      .  
23 2014-01-01  24    1125.3
24 2014-01-02  1     1213.3
.    .         .      .

当我这样绘制时

plt.plot(energy['Date'], energy['Energy'])
plt.xlabel('Date')
plt.ylabel('Energy')

我得到以下输出

这个图表没有多大意义,因为我无法观察到任何趋势。相反,我想绘制每天的平均能量。欢迎任何其他关于如何以我观察到任何趋势的方式绘制这个时间序列的建议

【问题讨论】:

标签: python python-3.x pandas matplotlib


【解决方案1】:

您需要 groupby 并首先聚合 mean

energy = energy.groupby('Date')['Energy'].mean()

然后Series.plot:

energy.plot()

大家一起:

energy.groupby('Date')['Energy'].mean().plot()

【讨论】:

    【解决方案2】:

    IIUC:

    你需要排序

    energy = energy.sort_values(['Date', 'Time'])
    plt.plot(energy['Date'], energy['Wind Generation'])
    plt.xlabel('Date')
    plt.ylabel('Energy')
    plt.autofmt_xdate()
    

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多