Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lag plot #1440

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions doc/source/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,22 @@ of the same class will usually be closer together and form larger structures.

@savefig andrews_curves.png width=6in
andrews_curves(data, 'Name')

Lag Plot
~~~~~~~~

Lag plots are used to check if a data set or time series is random. Random
data should not exhibit any structure in the lag plot. Non-random structure
implies that the underlying data are not random.

.. ipython:: python

from pandas.tools.plotting import lag_plot

plt.figure()

data = Series(0.1 * np.random.random(1000) +
0.9 * np.sin(np.linspace(-99 * np.pi, 99 * np.pi, num=1000)))

@savefig lag_plot.png width=6in
lag_plot(data)
5 changes: 5 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def test_kde(self):
ax = self.ts.plot(kind='kde', logy=True)
self.assert_(ax.get_yscale() == 'log')

@slow
def test_lag_plot(self):
from pandas.tools.plotting import lag_plot
_check_plot_works(lag_plot, self.ts)

class TestDataFramePlots(unittest.TestCase):

@classmethod
Expand Down
24 changes: 24 additions & 0 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ def random_color(column):
ax.grid()
return ax

def lag_plot(series, ax=None, **kwds):
"""Lag plot for time series.

Parameters:
-----------
series: Time series
ax: Matplotlib axis object, optional
kwds: Matplotlib scatter method keyword arguments, optional

Returns:
--------
ax: Matplotlib axis object
"""
import matplotlib.pyplot as plt
data = series.values
y1 = data[:-1]
y2 = data[1:]
if ax == None:
ax = plt.gca()
ax.set_xlabel("y(t)")
ax.set_ylabel("y(t + 1)")
ax.scatter(y1, y2, **kwds)
return ax

def grouped_hist(data, column=None, by=None, ax=None, bins=50, log=False,
figsize=None, layout=None, sharex=False, sharey=False,
rot=90):
Expand Down