A special kind of scatter plot is used to draw two variables X & Y
with lag. One set of observations X
is plotted in time series parallel to the second Y
.
pandas.plotting.lag_plot()
functionThe lag_plot()
function is used in pandas to draw a lag plot. It takes a time series, a lag, a matplotlib
axis object, and some additional keyword argument values. This method returns a matplotlib.axis.Axes
instance.
pandas.plotting.lag_plot(series, lag=1, ax=None, **kwds)
It takes the following argument values.
series
: This is an instance of a time series.lag
: This is an integer value, Default=1
, which shows a lag between every point of the scatter plot.ax
: This is a matplotlib axis object, Default=None
.**kwds
: These are the additional arguments for the scatter plot.It returns an instance of the matplotlib.axis.Axes
module.
In this code snippet, we create use the pandas.plotting.lag_plot()
function to create a lag plot.
# import python libraries in programimport pandas as pdimport numpy as npimport matplotlib.pyplot as plt# create a ndarray of cummulative sum of numbersx = np.cumsum(np.random.normal(loc=1, scale=5, size=25))# create a series of above created random valuess = pd.Series(x)# invoke lag_plot() function to draw a scaller plotpd.plotting.lag_plot(s, lag=3)# save output image as pngplt.savefig("output/lagplot.png")
pd.Series(x)
function to create a series of the above-created cumulative values.s
as series and lag=3
.