Open In App

Matplotlib.pyplot.xcorr() in Python

Last Updated : 22 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is built on NumPy and sideby framework that’s why it is fast and efficient. It is open-source and has huge community support. It possesses the ability to work well with many operating systems and graphic backends. To get what matplotlib.pyplot.xcorr() do we need to understand Cross-Correlation.
 

Cross Correlation

The correlation coefficient is a statistical measure of the strength of the relationship between the relative movements of two variables.
For example: Let us take two real valued functions f and g. g is at x is the difference along x axis. Now to calculate x ne use Cross Correlation. 
 

 

matplotlib.pyplot.xcorr()

matplotlib.pyplot.xcorr() function plots cross correlation between two array lists.
Parameters :

 

Parameter Input type Description
x A vector of real or complex floating point numbers. First variable for cross correlation.
y a vector of real or complex floating point numbers. The default value is x. Second variable for cross correlation.
detrend callable x and y are detrended by the detrend callable. This must be a function x = detrend(x) accepting and returning an numpy.array. It is optional parameter default is no normalization.
normed bool If True, input vectors are normalised to unit length.
usevlines bool If True, vertical lines are plotted from 0 to the xcorr value using Axes. It is an optional parameter
maxlags int Number of lags to show. If None, will return all 2 * len(x) – 1 lags. Optional parameter, default value is 10.

Return :

 

Parameter Type Description
lags array (length 2*maxlags+1) The lag vector.
c array (length 2*maxlags+1) The auto correlation vector.
line Line Collection or Line2D Artist added to the axes of the correlation: 
1. Line Collection if use lines is True. 
2. Line2D if use lines is False.
b Line2D or None Horizontal line at 0 if use lines is True None use lines is False.

Example 1: 
 

Python3




# import matplotlib library
import matplotlib.pyplot as plt
import numpy as np
 
# float lists for cross
# correlation
x=[11.37, 14.23, 16.3, 12.36,
   6.54, 4.23, 19.11, 12.13,
   19.91, 11.00]
 
y=[15.21, 12.23, 4.76, 9.89,
   8.96, 19.26, 12.24, 11.54,
   13.39, 18.96]
 
# Plot graph
fig = plt.figure()
ax1 = fig.add_subplot(211)
 
# cross correlation using
# xcorr() function
ax1.xcorr(x, y, usevlines=True,
          maxlags=5, normed=True,
          lw=2)
# adding grid to the graph
ax1.grid(True)
ax1.axhline(0, color='blue', lw=2)
 
# show final plotted graph
plt.show()


Output : 
 

matplotlib.pyplot.xcorr()

Example 2: 
 

Python3




# import matplotlib library
import matplotlib.pyplot as plt
import numpy as np
 
# float lists for cross
# correlation
x, y = np.random.randn(2, 100)
 
# Plot graph
fig = plt.figure()
ax1 = fig.add_subplot(211)
 
# cross correlation using xcorr()
# function
ax1.xcorr(x, y, usevlines=True,
          maxlags=50, normed=True,
          lw=2)
 
# adding grid to the graph
ax1.grid(True)
ax1.axhline(0, color='blue', lw=2)
 
# show final plotted graph
plt.show()


Output : 
 

matplotlib.pyplot.xcorr()

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads