Skip to content

Commit bbada0c

Browse files
committed
ENH: commit autocorrelation_plot by hand
1 parent ec4fa6f commit bbada0c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pandas 0.8.0
8383
- Add new ``qcut`` for cutting with quantiles (#1378)
8484
- Add ``value_counts`` top level array method (#1392)
8585
- Added Andrews curves plot tupe (#1325)
86+
- Add lag plot (#1440)
87+
- Add autocorrelation_plot (#1425)
8688
- Add support for tox and Travis CI (#1382)
8789
- Add support for ordered factors and use in GroupBy (#292)
8890

pandas/tools/plotting.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,42 @@ def lag_plot(series, ax=None, **kwds):
202202
ax.scatter(y1, y2, **kwds)
203203
return ax
204204

205+
def autocorrelation_plot(series, ax=None):
206+
"""Autocorrelation plot for time series.
207+
208+
Parameters:
209+
-----------
210+
series: Time series
211+
ax: Matplotlib axis object, optional
212+
213+
Returns:
214+
-----------
215+
ax: Matplotlib axis object
216+
"""
217+
import matplotlib.pyplot as plt
218+
n = len(series)
219+
data = series.values
220+
if ax == None:
221+
ax = plt.gca(xlim=(1, n), ylim=(-1.0, 1.0))
222+
mean = np.mean(data)
223+
c0 = np.sum((data - mean) ** 2) / float(n)
224+
def r(h):
225+
return ((data[:n - h] - mean) * (data[h:] - mean)).sum() / float(n) / c0
226+
x = np.arange(n) + 1
227+
y = map(r, x)
228+
z95 = 1.959963984540054
229+
z99 = 2.5758293035489004
230+
ax.axhline(y=z99/np.sqrt(n), linestyle='--', color='grey')
231+
ax.axhline(y=z95/np.sqrt(n), color='grey')
232+
ax.axhline(y=0.0, color='black')
233+
ax.axhline(y=-z95/np.sqrt(n), color='grey')
234+
ax.axhline(y=-z99/np.sqrt(n), linestyle='--', color='grey')
235+
ax.set_xlabel("Lag")
236+
ax.set_ylabel("Autocorrelation")
237+
ax.plot(x, y)
238+
ax.grid()
239+
return ax
240+
205241
def grouped_hist(data, column=None, by=None, ax=None, bins=50, log=False,
206242
figsize=None, layout=None, sharex=False, sharey=False,
207243
rot=90):

0 commit comments

Comments
 (0)