绘制Pandas系列数据的平滑曲线

时间:2014-11-24 02:27:12

标签: python pandas plot interpolation

我的数据是:

>>> ts = pd.TimeSeries(data,indexconv)
>>> tsgroup = ts.resample('t',how='sum')
>>> tsgroup
2014-11-08 10:30:00    3
2014-11-08 10:31:00    4
2014-11-08 10:32:00    7
  [snip]
2014-11-08 10:54:00    5
2014-11-08 10:55:00    2
Freq: T, dtype: int64
>>> tsgroup.plot()
>>> plt.show()

indexconv是使用datetime.strptime转换的字符串。

情节很像这样(这些不是我的实际情节): enter image description here

我怎样才能让它像这样平滑: enter image description here

我知道this article中提及的scipy.interpolate(这是我从中获取图片的地方),但我如何将其应用于Pandas时间序列?

我找到了一个名为Vincent的优秀图书馆来处理Pandas,但它并不支持Python 2.6。

3 个答案:

答案 0 :(得分:5)

知道了。在this question的帮助下,这就是我所做的:

  1. 将我的tsgroup从几分钟重新取样到几秒钟。

    \>>> tsres = tsgroup.resample('S')
    \>>> tsres
    2014-11-08 10:30:00     3
    2014-11-08 10:30:01   NaN
    2014-11-08 10:30:02   NaN
    2014-11-08 10:30:03   NaN
    ...
    2014-11-08 10:54:58   NaN
    2014-11-08 10:54:59   NaN
    2014-11-08 10:55:00     2
    Freq: S, Length: 1501
  2. 使用.interpolate(method='cubic')插入数据。这会将数据传递给scipy.interpolate.interp1d并使用cubic类型,因此您需要安装scipy(pip install scipy 1

    \>>> tsint = tsres.interpolate(method='cubic')
    \>>> tsint
    2014-11-08 10:30:00    3.000000
    2014-11-08 10:30:01    3.043445
    2014-11-08 10:30:02    3.085850
    2014-11-08 10:30:03    3.127220
    ...
    2014-11-08 10:54:58    2.461532
    2014-11-08 10:54:59    2.235186
    2014-11-08 10:55:00    2.000000
    Freq: S, Length: 1501
  3. 使用tsint.plot()绘制它。这是原始tsgrouptsint之间的比较:

  4. 1 如果您从.interpolate(method='cubic')收到错误,告诉您即使安装了Scipy也未安装,请打开{{1}或者您的文件可能位于何处,并将第二行从/usr/lib64/python2.6/site-packages/scipy/interpolate/polyint.py更改为from scipy import factorial

答案 1 :(得分:0)

答案 2 :(得分:0)

您还可以使用移动平均值平滑数据,从而对数据有效地应用低通滤波器。熊猫通过rolling()方法支持这一点。

相关问题