What is matplotlib.pyplot.acorr() funtion in Python?

Digital Image Processing is a significant aspect of data science. It is used in image modification and enhancement to acquire image attributes that lead to a greater understanding of data.

matplotlib is a Python library that provides a multitude of functions to visualize and plot data. The matplotlib.pyplot.acorr() function is used to plot the autocorrelation of data and is used mainly in data analysis.

Syntax

matplotlib.pyplot.acorr(x, *, data=None, **kwargs)

Parameters

  1. x: A sequence of scalars. This parameter is required.

  2. detrend: A callable function that detrends x. The default is mlab.detrend_none. (Optional)

  3. normed: A Boolean value that, if set to True, normalizes x to unit length. The default is True. (Optional)

  4. usevlines: A Boolean value that, if set to True, plots vertical lines from 0 to the acorr values. The default is True. (Optional)

  5. maxlags: An integer value that specifies the number of lags to show. The default is 10. (Optional)

  6. linestyle: A Line2D object that specifies the line style for plotting points. Used only when usevlines is False. (Optional)

  7. marker: A string value that specifies the marker for plotting points. Used only when usevlines is False. (Optional)

Return value

The method returns a tuple (lags, c, line, b). lags is the lag vector, c is the correlation vector, line is a LineCollection object if usevlines is True, and b is the horizontal line at y=0 if usevlines is True.

Code example

#import libraries
import matplotlib.pyplot as plt
import numpy as np
#initialize data
x = np.array([14.8, 61.7, 4.3, 17.2, 53.7, 19.0, 24.3, 13.7])
#plot correlation
plt.acorr(x,
normed = True,
maxlags = 5)
#add labels
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
#show the autocorrelation plot
plt.show()

The code plots an autocorrelation for the specified data as seen below.

widget

Free Resources