hvplot.plotting.lag_plot#

hvplot.plotting.lag_plot(data, lag=1, **kwds)[source]#

Lag plot for time series.

A lag plot is a scatter plot of a time series against a lag of itself. It helps in visualizing the temporal dependence between observations by plotting the values at time t on the x-axis and the values at time t + lag on the y-axis.

Parameters:
dataSeries or DataFrame

The time series to visualize.

lagint, optional

Lag length of the scatter plot. Default is 1.

**kwdsoptional

hvplot.scatter options

Returns:
objHoloViews object

The HoloViews representation of the plot.

Examples#

Simple lag plot#

Lag plots are most commonly used to look for patterns in time series data. This example shows a lag plot constructed from a time series. The lag plot suggests that the time series exhibits positive autocorrelation at a lag of 1 (the default setting).

import hvplot.pandas  # noqa
import numpy as np
import pandas as pd

np.random.seed(42)
series = pd.Series(np.cumsum(np.random.randn(100)))

series.hvplot.line(title="Time series", frame_width=300) +\
hvplot.plotting.lag_plot(series, title="Lag plot", frame_width=300)

With multiple variables#

In this example, we use a lag of 52 to compare annual patterns, as the dataset contains weekly data.

import hvplot

df = hvplot.sampledata.stocks("pandas").set_index("date")

hvplot.plotting.lag_plot(
    df.loc["2020":], lag=52, data_aspect=1,
    xlim=(0, 6), ylim=(0, 6)
)

Use of lag#

For this dataset built from a random walk, the autocorrelation is maximum with a lag of 1.

import hvplot
import numpy as np
import pandas as pd

np.random.seed(42)
series = pd.Series(np.cumsum(np.random.randn(300)))

plot_opts = dict(frame_width=200)
hvplot.plotting.lag_plot(series, lag=1, title="lag=1 (default)", **plot_opts) +\
hvplot.plotting.lag_plot(series, lag=5, title="lag=5", **plot_opts) +\
hvplot.plotting.lag_plot(series, lag=100, title="lag=50", **plot_opts)

Different kinds of autocorrelation#

import hvplot
import numpy as np
import pandas as pd

np.random.seed(42)
df = pd.DataFrame({
    "positive": np.cumsum(np.random.randn(100)),
    "noise": np.random.randn(100),
    "cyclic": np.sin(np.linspace(0, 4 * np.pi, 100))
})

plot_opts = dict(frame_width=200, shared_axes=False)
hvplot.plotting.lag_plot(df["noise"], title="No autocorrelation", **plot_opts) +\
hvplot.plotting.lag_plot(df["positive"], title="Positive autocorrelation", **plot_opts) +\
hvplot.plotting.lag_plot(df["cyclic"], title="Cyclic autocorrelation", **plot_opts)
This web page was generated from a Jupyter notebook and not all interactivity will work on this website.