Autocorrelation plot in matplotlib Python

Here we will learn about “Autocorrelation plot in matplotlib in Python”. In this topic, we will see what is the use or importance of the function plt.acorr() and how data is correlated to another form of data in Python using the autocorrelation plot in matplotlib library. So, kindly go through this text to learn and explore this function in Python.

Autocorrelation plot:

An autocorrelation plot is implemented to show the relationship between present values and old values of data, whether these values correlate with one another or not. An autocorrelation plot is plotted to show the correlation between data that have variations in time or lag in time. The range of an autocorrelation plot is from -1 to +1. Autocorrelation +1 indicates that there is a direct proportion between the data values of the two distinct time series, i.e… if one time series increases another also increases and vice-versa. Autocorrelation -1 indicates that there is an indirect proportion between the data values of the two distinct time series, i.e… if one time series increases other decreases, and vice-versa.

Requirements:

To plot an autocorrelation plot we require the following libraries in our system and code space. The installation command for the libraries is also given below.

# Matplotlib library
pip install matplotlib
# NumPy library
pip install numpy

Example 1:

# import matplotlib library
from matplotlib import pyplot as plt
# import numpy library 
import numpy as np
# random array
data=np.random.randn(20)
# function for autocorrelation plot
plt.acorr(data)
# This function shows the title of the plot
plt.title("Autocorrelation Plot")
# This function shows the x label
plt.xlabel("time lag")
# This function is used to display the grid lines
plt.grid(True)
# This function shows the plot
plt.show()

Output:

Autocorrelation plot in matplotlib Python

Example 2:

# import matplotlib library
from matplotlib import pyplot as plt
# import numpy library 
import numpy as np
# random array
data=np.random.rand(11)
# function for autocorrelation plot
plt.acorr(data)
# This function shows the title of the plot
plt.title("Autocorrelation Plot")
# This function shows the x label
plt.xlabel("time lag")
# This function is used to display the grid lines
plt.grid(True)
# This function shows the plot
plt.show()

Output:

plt.acorr()

Maxlags:

You can also adjust the number of lags in your autocorrelation plot as follows.

# import matplotlib library
from matplotlib import pyplot as plt
# import numpy library 
import numpy as np
# random array
data=np.random.rand(21)
# function for autocorrelation plot
plt.acorr(data,maxlags=20)
# This function shows the title of the plot
plt.title("Autocorrelation Plot")
# This function shows the x label
plt.xlabel("time lag")
# This function is used to display the grid lines
plt.grid(True)
# This function shows the plot
plt.show()

Output:

Maxlags in matplotlib

Leave a Reply

Your email address will not be published. Required fields are marked *