Autocorrelation Plot Python
In this article, you will learn how to plot autocorrelation in Python. First, it is important to know about the term autocorrelation.
The analysis of autocorrelation is a numerical instrument for discovering repeating patterns, for example, the presence of an periodic sign clouded by noise, or distinguishing the missing fundamental frequency in a signal suggested by its consonant frequencies.
Features of Autocorrelation
- Autocorrelation measures a set of current values against a set of past values and finds whether they correlate.
- It is the correlation of one time series data to another time series data which has a time lag.
- It varies from +1 to -1.
- An auto correlation of +1 indicates that if the time series one increases in value the time series 2 also increases in proportion to the change in time series 1.
- An auto correlation of -1 indicates that if the time series one increases in value the time series 2 decreases in proportion to the change in time series 1.
- Some applications in which correlation is used are in signal detection, pattern recognition, technical analysis of stocks and so on.
We hope, you already know about the matplotlib library of Python. This module provides an interface Pyplot which is a state-based interface. The acorr() function in pyplot module of matplotlib library is used to plot the autocorrelation. Here is the syntax -
matplotlib.pyplot.acorr(x, *, data=None, **kwargs)
- x - array-like, a sequence of scalar,
- detrend - optional, default value is mlab.detrend_none,
- normed - optional, default value is True,
- usevlines - optional, default value is True,
- maxlags - optional, default value is 10,
- linestyle - optional, used for plotting the data points, only when usevlines is False,
- marker - optional, default value is string 'o'.
- lags are a length 2`maxlags+1 lag vector.
- c is the 2`maxlags+1 auto correlation vectorI.
- line is a Line2D instance returned by plot.
- b is the x-axis.
Python Autocorrelation Example 1
The following code displays autocorrelation plot using matplotlib -
import matplotlib.pyplot as plot import numpy as np # Time series data data = np.array([22.40,20.25,10.05,21.02,17.70, 7.70,14.50,22.78,32.90,14.30]) # plot autocorrelation plot.acorr(data, usevlines=True, normed=True, maxlags=9, lw=2) # autocorrelation plot labels plot.title('Autocorrelation plot') plot.xlabel('Lags') plot.ylabel('Autocorrelation') plot.grid(True) # display the autocorrelation plot plot.show()
The above code returns the following output -
Python Autocorrelation Example 2
import matplotlib.pyplot as plot import numpy as np # Create some random seed for the random state np.random.seed(42) # creating some random data data = np.random.randn(30) # plot autocorrelation plot.acorr(data, usevlines=True, normed=True, maxlags=9, lw=2) # autocorrelation plot labels plot.title('Autocorrelation plot') plot.xlabel('Lags') plot.ylabel('Autocorrelation') plot.grid(True) # display the autocorrelation plot plot.show()
We will get the following output -
Related ArticlesVader Sentiment Analysis Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
How to generate QR Code in Python using PyQRCode
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml