Python 抑制绘图并仅获取结果

Python 抑制绘图并仅获取结果,python,matplotlib,Python,Matplotlib,我试图将两个信号对齐,以获得提供最佳系数的滞后。我正在使用matplotlib中的xcorr函数。我只对以下代码中的z感兴趣 有没有办法抑制绘图(我不想要绘图)并只得到结果 from matplotlib.pyplot import xcorr z = xcorr([1.,2.,3.,4.,5.], [0,0,0,0,1.], normed=False, maxlags=4) lagsOut = list(z[0]) corrCoeff = list(z[1]) 感谢matplotlib是

我试图将两个信号对齐,以获得提供最佳系数的滞后。我正在使用matplotlib中的
xcorr
函数。我只对以下代码中的
z
感兴趣

有没有办法抑制绘图(我不想要绘图)并只得到结果

from matplotlib.pyplot import xcorr
z = xcorr([1.,2.,3.,4.,5.], [0,0,0,0,1.], normed=False, maxlags=4)
lagsOut = list(z[0])  
corrCoeff = list(z[1])

感谢

matplotlib
是一个绘图模块。如果不想打印,最好直接使用
numpy
。看

如果您还需要
xcorr
中的任何内容,可以使用
inspect.getsource
查看它的功能。这里是一个简略的转储:

def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none,
          usevlines=True, maxlags=10, **kwargs):
        Nx = len(x)
        if Nx != len(y):
            raise ValueError('x and y must be equal length')

        x = detrend(np.asarray(x))
        y = detrend(np.asarray(y))

        c = np.correlate(x, y, mode=2)

        if normed:
            c /= np.sqrt(np.dot(x, x) * np.dot(y, y))

        if maxlags is None:
            maxlags = Nx - 1

        if maxlags >= Nx or maxlags < 1:
            raise ValueError('maglags must be None or strictly '
                             'positive < %d' % Nx)

        lags = np.arange(-maxlags, maxlags + 1)
        c = c[Nx - 1 - maxlags:Nx + maxlags]

        if usevlines:
            a = self.vlines(lags, [0], c, **kwargs)
            b = self.axhline(**kwargs)
        else:

            kwargs.setdefault('marker', 'o')
            kwargs.setdefault('linestyle', 'None')
            a, = self.plot(lags, c, **kwargs)
            b = None
        return lags, c, a, b
def xcorr(self,x,y,normed=True,detrend=mlab.detrend_none,
usevlines=True,maxlags=10,**kwargs):
Nx=len(x)
如果Nx!=len(y):
raise VALUE ERROR('x和y的长度必须相等')
x=趋势(np.asarray(x))
y=detrend(np.asarray(y))
c=np.相关(x,y,模式=2)
如果规范化:
c/=np.sqrt(np.dot(x,x)*np.dot(y,y))
如果maxlags为无:
maxlags=Nx-1
如果maxlags>=Nx或maxlags<1:
raise VALUE ERROR('磁痕必须为无或严格为'
'正<%d'%Nx)
lags=np.arange(-maxlags,maxlags+1)
c=c[Nx-1-maxlags:Nx+maxlags]
如果使用以下线路:
a=自身尺寸线(滞后[0],c,**kwargs)
b=self.axhline(**kwargs)
其他:
setdefault('marker','o')
kwargs.setdefault('linestyle','None')
a、 =自绘制(滞后,c,**kwargs)
b=无
返回滞后,c,a,b

matplotlib
是一个绘图模块。如果不想打印,最好直接使用
numpy
。看

如果您还需要
xcorr
中的任何内容,可以使用
inspect.getsource
查看它的功能。这里是一个简略的转储:

def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none,
          usevlines=True, maxlags=10, **kwargs):
        Nx = len(x)
        if Nx != len(y):
            raise ValueError('x and y must be equal length')

        x = detrend(np.asarray(x))
        y = detrend(np.asarray(y))

        c = np.correlate(x, y, mode=2)

        if normed:
            c /= np.sqrt(np.dot(x, x) * np.dot(y, y))

        if maxlags is None:
            maxlags = Nx - 1

        if maxlags >= Nx or maxlags < 1:
            raise ValueError('maglags must be None or strictly '
                             'positive < %d' % Nx)

        lags = np.arange(-maxlags, maxlags + 1)
        c = c[Nx - 1 - maxlags:Nx + maxlags]

        if usevlines:
            a = self.vlines(lags, [0], c, **kwargs)
            b = self.axhline(**kwargs)
        else:

            kwargs.setdefault('marker', 'o')
            kwargs.setdefault('linestyle', 'None')
            a, = self.plot(lags, c, **kwargs)
            b = None
        return lags, c, a, b
def xcorr(self,x,y,normed=True,detrend=mlab.detrend_none,
usevlines=True,maxlags=10,**kwargs):
Nx=len(x)
如果Nx!=len(y):
raise VALUE ERROR('x和y的长度必须相等')
x=趋势(np.asarray(x))
y=detrend(np.asarray(y))
c=np.相关(x,y,模式=2)
如果规范化:
c/=np.sqrt(np.dot(x,x)*np.dot(y,y))
如果maxlags为无:
maxlags=Nx-1
如果maxlags>=Nx或maxlags<1:
raise VALUE ERROR('磁痕必须为无或严格为'
'正<%d'%Nx)
lags=np.arange(-maxlags,maxlags+1)
c=c[Nx-1-maxlags:Nx+maxlags]
如果使用以下线路:
a=自身尺寸线(滞后[0],c,**kwargs)
b=self.axhline(**kwargs)
其他:
setdefault('marker','o')
kwargs.setdefault('linestyle','None')
a、 =自绘制(滞后,c,**kwargs)
b=无
返回滞后,c,a,b

使用
np.关联

import numpy as np
x = [1., 2., 3., 4., 5.]
y = [0, 0, 0, 0, 1.]
corrCoef = np.correlate(x, y, 'full')
lagsOut = np.arange(-len(x)+1, len(x))

使用
np.correlate

import numpy as np
x = [1., 2., 3., 4., 5.]
y = [0, 0, 0, 0, 1.]
corrCoef = np.correlate(x, y, 'full')
lagsOut = np.arange(-len(x)+1, len(x))
正如我在这里回答的那样, 有时我们不能使用
numpy.correlate
,因为有时我们需要仅由
matplotlib.xcorr
提供的maxlags参数。但我知道,如果我们将复杂数据类型作为参数直接放入
matplotlib.xcorr
中,当matplotlib尝试绘制绘图时,我们将收到“将复杂数据类型转换为真实数据类型”警告。 下面我修改了matplotlib中的代码,因此它可以用作独立函数

<!-- language: python -->

import numpy as np
import matplotlib.pyplot as plt

def xcorr(x, y, maxlags=10):
    Nx = len(x)
    if Nx != len(y):
        raise ValueError('x and y must be equal length')

    c = np.correlate(x, y, mode=2)

    if maxlags is None:
        maxlags = Nx - 1

    if maxlags >= Nx or maxlags < 1:
        raise ValueError('maxlags must be None or strictly positive < %d' % Nx)

    c = c[Nx - 1 - maxlags:Nx + maxlags]

    return c

将numpy作为np导入
将matplotlib.pyplot作为plt导入
def xcorr(x,y,最大值=10):
Nx=len(x)
如果Nx!=len(y):
raise VALUE ERROR('x和y的长度必须相等')
c=np.相关(x,y,模式=2)
如果maxlags为无:
maxlags=Nx-1
如果maxlags>=Nx或maxlags<1:
raise VALUETERROR('最大值必须为无或严格为正<%d'%Nx)
c=c[Nx-1-maxlags:Nx+maxlags]
返回c
正如我在这里回答的那样, 有时我们不能使用
numpy.correlate
,因为有时我们需要仅由
matplotlib.xcorr
提供的maxlags参数。但我知道,如果我们将复杂数据类型作为参数直接放入
matplotlib.xcorr
中,当matplotlib尝试绘制绘图时,我们将收到“将复杂数据类型转换为真实数据类型”警告。 下面我修改了matplotlib中的代码,因此它可以用作独立函数

<!-- language: python -->

import numpy as np
import matplotlib.pyplot as plt

def xcorr(x, y, maxlags=10):
    Nx = len(x)
    if Nx != len(y):
        raise ValueError('x and y must be equal length')

    c = np.correlate(x, y, mode=2)

    if maxlags is None:
        maxlags = Nx - 1

    if maxlags >= Nx or maxlags < 1:
        raise ValueError('maxlags must be None or strictly positive < %d' % Nx)

    c = c[Nx - 1 - maxlags:Nx + maxlags]

    return c

将numpy作为np导入
将matplotlib.pyplot作为plt导入
def xcorr(x,y,最大值=10):
Nx=len(x)
如果Nx!=len(y):
raise VALUE ERROR('x和y的长度必须相等')
c=np.相关(x,y,模式=2)
如果maxlags为无:
maxlags=Nx-1
如果maxlags>=Nx或maxlags<1:
raise VALUETERROR('最大值必须为无或严格为正<%d'%Nx)
c=c[Nx-1-maxlags:Nx+maxlags]
返回c

FYI如果要关联复杂数据向量,请确保共轭其中一个输入向量(源);如果要关联复杂数据向量,请确保共轭其中一个输入向量(源和)