Pandas Plot Graph Plots Series Data
My details:
>>> 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
are converted to strings with datetime.strptime
.
The plot is very edgy like this (these are not my actual plots):
How can I smooth it out like this:
I know about the scipy.interpolate
one mentioned in this article (where I got the images from), but how can I apply it to Pandas time series?
I found this great library called Vincent that deals with Pandas but does not support Python 2.6.
source to share
Got it. Using this question , here's what I did:
-
Repeat mine
tsgroup
for 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
-
Interpolate the data using
.interpolate(method='cubic')
. This passes data toscipy.interpolate.interp1d
and uses the viewcubic
, so you need to set scipy (pip install scipy
) to 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
-
Divide it with
tsint.plot()
. Here's a comparison of the originaltsgroup
andtsint
:
1 If you get an error message .interpolate(method='cubic')
telling you that Scipy is not installed even though you have it installed, open /usr/lib64/python2.6/site-packages/scipy/interpolate/polyint.py
or wherever your file might be and change the second line from from scipy import factorial
to from scipy.misc import factorial
.
source to share