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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27097015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:42:07  来源:igfitidea点击:

Plot smooth curves of Pandas Series data

pythonpandasplotinterpolation

提问by Alaa Ali

My data is:

我的数据是:

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

indexconvare strings converted using datetime.strptime.

indexconv是使用datetime.strptime.

The plot is very edgy like this (these aren't my actual plots): enter image description here

情节很像这样(这些不是我的实际情节): 在此处输入图片说明

How can I smooth it out like this: enter image description here

我怎样才能像这样平滑它: 在此处输入图片说明

I know about scipy.interpolatementioned in this article(which is where I got the images from), but how can I apply it for Pandas time series?

我知道这篇文章中scipy.interpolate提到的(这是我从那里获得图像的地方),但是我如何将它应用于 Pandas 时间序列?

I found this great library called Vincentthat deals with Pandas, but it doesn't support Python 2.6.

我发现了一个名为Vincent 的很棒的库,它可以处理 Pandas,但它不支持 Python 2.6。

采纳答案by Alaa Ali

Got it. With help from this question, here's what I did:

知道了。在这个问题的帮助下,就是我所做的:

  1. Resample my tsgroupfrom minutes to seconds.

    \>>> 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 the data using .interpolate(method='cubic'). This passes the data to scipy.interpolate.interp1dand uses the cubickind, so you need to have scipy installed (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. Plot it using tsint.plot(). Here's a comparison between the original tsgroupand tsint:

  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(). 这是原始tsgroup和之间的比较tsint

1If you're getting an error from .interpolate(method='cubic')telling you that Scipy isn't installed even if you do have it installed, open up /usr/lib64/python2.6/site-packages/scipy/interpolate/polyint.pyor wherever your file might be and change the second line from from scipy import factorialto from scipy.misc import factorial.

1如果你从得到一个错误.interpolate(method='cubic'),告诉你,就算你安装了它,打开SciPy的未安装/usr/lib64/python2.6/site-packages/scipy/interpolate/polyint.py或任何你的文件可能是从改变第二行from scipy import factorialfrom scipy.misc import factorial

回答by Marcus

You can smooth out your data with moving averages as well, effectively applying a low-pass filter to your data. Pandas supports this with the rolling()method.

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