Autocorrelation Plot Python
In this article, you will learn how to plot autocorrelation using the Python programming language. 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 a 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 that 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.
Python Autocorrelation
We hope you already know about the matplotlib library of Python. This module provides an interface to Pyplot which is a state-based interface. The acorr() function in the pyplot module of the matplotlib library is used to plot the autocorrelation. Here is the syntax-
matplotlib.pyplot.acorr(x, *, data=None, **kwargs)
Parameters:
- x- array-like, a sequence of scalars.
- detrend- optional, the default value is mlab.detrend_none.
- normed- optional, the default value is True.
- usevlines- optional, the default value is True.
- maxlags- optional, the default value is 10.
- linestyle- optional, used for plotting the data points, only when usevlines is False.
- marker- optional, the 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 an 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 Articles
Simple Matplotlib Animation ExampleMatplotlib Pie Chart
Python Line Plot Using Matplotlib
Python Matplotlib 3D Plot
How to save figure in matplotlib pyplot
Python Matplotlib Scatter Plot
Python Pandas Plotting
Vader Sentiment Analysis Python
isalpha Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
Lemmatization nltk
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