📜  Python中的 Matplotlib.pyplot.xcorr()

📅  最后修改于: 2022-05-13 01:55:35.773000             🧑  作者: Mango

Python中的 Matplotlib.pyplot.xcorr()

Matplotlib 建立在 NumPy 和 sideby 框架之上,这就是它快速高效的原因。它是开源的,并拥有巨大的社区支持。它具有与许多操作系统和图形后端一起工作的能力。为了得到什么 matplotlib.pyplot.xcorr() 我们需要了解互相关。

互相关

相关系数是对两个变量的相对运动之间关系强度的统计量度。
例如:让我们取两个实值函数 f 和 g。 g 在 x 是沿 x 轴的差。现在计算 x ne 使用互相关。

matplotlib.pyplot.xcorr()

matplotlib.pyplot.xcorr()函数绘制两个数组列表之间的互相关。
参数 :

ParameterInput typeDescription
xA vector of real or complex floating point numbers.First variable for cross correlation.
ya vector of real or complex floating point numbers. The default value is x.Second variable for cross correlation.
detrendcallablex 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.
normedboolIf True, input vectors are normalised to unit length.
usevlinesboolIf True, vertical lines are plotted from 0 to the xcorr value using Axes. It is an optional parameter
maxlagsintNumber of lags to show. If None, will return all 2 * len(x) – 1 lags. Optional parameter, default value is 10.

返回 :

ParameterTypeDescription
lagsarray (length 2*maxlags+1)The lag vector.
carray (length 2*maxlags+1)The auto correlation vector.
lineLine Collection or Line2DArtist added to the axes of the correlation: 
1. Line Collection if use lines is True. 
2. Line2D if use lines is False.
bLine2D or NoneHorizontal line at 0 if use lines is True None use lines is False.

示例 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()


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()


输出 :

matplotlib.pyplot.xcorr()

示例 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()

输出 :

matplotlib.pyplot.xcorr()