绘制具有大量数据点的时间序列matplotlib

时间:2017-02-11 13:00:39

标签: python python-3.x pandas matplotlib

我想绘制一个数据集的时间序列,该数据集的数据为12个月。但是,12个月的每天每小时都会记录数据。整个数据集超过8000个数据点。数据采用以下格式

    public MainWindow()
    {
        InitializeComponent();
        var h1 = new Header()
        {
            Name = "Name0",
            Value = true,
            Names = new string[2] { "Name1", "Name2" }
        };
        var h2 = new Header()
        {
            Name = "Name1",
            Value = true,
            Names = new string[2] { "Name12", "Name22" }
        };

        addNewColumn(h1, "col1");
        addNewColumn(h2, "col2");

    }

    public class Header
    {
         public string Name { get; set; }
         public bool Value { get; set; }
         public string[] Names { get; set; }
    }

当我像这样绘制时

        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
.    .         .      .

我得到以下输出 this question

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

2 个答案:

答案 0 :(得分:3)

首先需要groupby聚合mean

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

然后Series.plot

energy.plot()

所有在一起:

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

答案 1 :(得分:1)

IIUC:

你需要排序

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