首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置Python pandas autocorrelation_plot中的滞后数

设置Python pandas autocorrelation_plot中的滞后数
EN

Stack Overflow用户
提问于 2016-07-21 19:43:48
回答 3查看 9.4K关注 0票数 13

代码

代码语言:javascript
复制
import numpy as np
from pandas.tools.plotting import autocorrelation_plot
import matplotlib.pyplot as plt
nobs = 10000
xx = np.random.normal(size=nobs)
autocorrelation_plot(xx)
plt.show()

绘制xx的自相关,但它绘制所有10000个滞后。如何只绘制前10个?

函数autocorrelation_plot的启动方式如下:

代码语言:javascript
复制
def autocorrelation_plot(series, ax=None, **kwds):
    """Autocorrelation plot for time series.
    Parameters:
    -----------
    series: Time series
    ax: Matplotlib axis object, optional
    kwds : keywords
        Options to pass to matplotlib plotting method

有没有办法使用**kwds参数设置绘制的滞后数?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-07-12 21:11:01

autocorrelation_plot返回一个matplotlib.axis对象。因此,您可以简单地使用set_xlim()方法来限制x轴:

代码语言:javascript
复制
autocorrelation_plot(xx).set_xlim([0, 10])

Reference

票数 5
EN

Stack Overflow用户

发布于 2019-07-03 03:01:21

如果不需要使用pandas方法,就像备份解决方案一样。有一个statsmodels函数plot_acf,您可以在其中设置lags参数。

代码语言:javascript
复制
from statsmodels.graphics.tsaplots import plot_acf
import pandas as pd
d = dict()
d['value'] = [11, 22, 34, 22, 43, 23, 45, 32, 56, 40, 44, 33, 22, 56, 44]
df = pd.DataFrame.from_dict(d)
plot_acf(df, lags = 5)
票数 3
EN

Stack Overflow用户

发布于 2017-07-29 15:02:32

autocorrelation_plot函数是一个高级函数。查看pandas库中的代码:

代码语言:javascript
复制
def autocorrelation_plot(series, ax=None, **kwds):
"""Autocorrelation plot for time series.

Parameters:
-----------
series: Time series
ax: Matplotlib axis object, optional
kwds : keywords
    Options to pass to matplotlib plotting method

Returns:
-----------
ax: Matplotlib axis object
"""
import matplotlib.pyplot as plt
n = len(series)
data = np.asarray(series)
if ax is None:
    ax = plt.gca(xlim=(1, n), ylim=(-1.0, 1.0))
mean = np.mean(data)
c0 = np.sum((data - mean) ** 2) / float(n)

def r(h):
    return ((data[:n - h] - mean) *
            (data[h:] - mean)).sum() / float(n) / c0
x = np.arange(n) + 1
y = lmap(r, x)
z95 = 1.959963984540054
z99 = 2.5758293035489004
ax.axhline(y=z99 / np.sqrt(n), linestyle='--', color='grey')
ax.axhline(y=z95 / np.sqrt(n), color='grey')
ax.axhline(y=0.0, color='black')
ax.axhline(y=-z95 / np.sqrt(n), color='grey')
ax.axhline(y=-z99 / np.sqrt(n), linestyle='--', color='grey')
ax.set_xlabel("Lag")
ax.set_ylabel("Autocorrelation")
ax.plot(x, y, **kwds)
if 'label' in kwds:
    ax.legend()
ax.grid()
return ax

函数中的所有行都缺少一个制表符。

添加到标题中:

代码语言:javascript
复制
from pandas.compat import lmap

在末尾前的第4行中,将ax.plot(x,y,**kwds)更改为ax.plot(x:10,y:10,**kwds)

我添加了一个n_samples变量:

代码语言:javascript
复制
from pandas.compat import lmap


def autocorrelation_plot(series, n_samples=None, ax=None, **kwds):
    """Autocorrelation plot for time series.

    Parameters:
    -----------
    series: Time series
    ax: Matplotlib axis object, optional
    kwds : keywords
        Options to pass to matplotlib plotting method

    Returns:
    -----------
    ax: Matplotlib axis object
    """
    import matplotlib.pyplot as plt
    n = len(series)
    data = np.asarray(series)
    if ax is None:
        ax = plt.gca(xlim=(1, n_samples), ylim=(-1.0, 1.0))
    mean = np.mean(data)
    c0 = np.sum((data - mean) ** 2) / float(n)

    def r(h):
        return ((data[:n - h] - mean) *
                (data[h:] - mean)).sum() / float(n) / c0
    x = (np.arange(n) + 1).astype(int)
    y = lmap(r, x)
    z95 = 1.959963984540054
    z99 = 2.5758293035489004
    ax.axhline(y=z99 / np.sqrt(n), linestyle='--', color='grey')
    ax.axhline(y=z95 / np.sqrt(n), color='grey')
    ax.axhline(y=0.0, color='black')
    ax.axhline(y=-z95 / np.sqrt(n), color='grey')
    ax.axhline(y=-z99 / np.sqrt(n), linestyle='--', color='grey')
    ax.set_xlabel("Lag")
    ax.set_ylabel("Autocorrelation")
    if n_samples:
        ax.plot(x[:n_samples], y[:n_samples], **kwds)
    else:
        ax.plot(x, y, **kwds)
    if 'label' in kwds:
        ax.legend()
    ax.grid()
    return ax
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38503381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档