【问题标题】:Unable to Modify the X Axis Tick Locator in ACF, PACF plots in statsmodels, Python无法修改 ACF 中的 X 轴刻度定位器,statsmodels 中的 PACF 图,Python
【发布时间】:2020-06-25 08:10:05
【问题描述】:

我想修改 ACF 和 PACF 图的 X 轴刻度,即我希望每 2 和 4 个单位(次要和主要刻度)后有一个刻度,而不是默认的 20 个单位。我正在尝试以下代码:

from statsmodels.graphics.tsaplots import plot_acf,plot_pacf

rcParams['figure.figsize']=20,10
ax = plt.subplot(211)
plot_acf(ts_log_diff) 
ax.xaxis.set_major_locator(plt.MultipleLocator=4) 
ax.xaxis.set_minor_locator(plt.MultipleLocator=2)
plt.subplot(212)
plot_pacf(ts_log_diff, ax=plt.gca())
plt.show()

我得到的错误信息是:

File "<ipython-input-99-bfa377e377fd>", line 5
ax.xaxis.set_major_locator(plt.MultipleLocator=4) 
                          ^
SyntaxError: keyword can't be an expression

我在 pd.plotting.autocorrelation_plot 中使用了类似的语法,它正在工作:

plotacf= pd.plotting.autocorrelation_plot(ts_log_diff)
plotacf.xaxis.set_major_locator(plt.MultipleLocator(2))
plotacf.xaxis.set_minor_locator(plt.MultipleLocator( 4))

【问题讨论】:

  • 我不确定其他代码,但是在Python中,你不能在关键字参数中使用点 - (plt.MultipleLocator=4)`你为什么不使用下面的?ax.xaxis .set_major_locator(plt.MultipleLocator(4))
  • 谢谢伙计。我的错。那是语法的问题。是的,这种语法有效:

标签: python matplotlib time-series advanced-custom-fields statsmodels


【解决方案1】:

为了结束这个问题,我从评论转到回答。

您的代码的问题是您使用了带点的关键字参数 - 这是无效的语法。但是您根本不需要使用关键字参数。相反,您需要调用以下代码:

ax.xaxis.set_major_locator(plt.MultipleLocator(4))

我建议您查看更多关于关键字参数的信息,以便您了解语法。

关键字参数示例:

比如你有一个函数:

from math import sqrt

def quadratic(a, b, c):
    x1 = -b / (2*a)
    x2 = sqrt(b**2 - 4*a*c) / (2*a)
    return (x1 + x2), (x1 - x2)

您可以调用quadratic(31, 93, 62) 或直接使用args 名称调用-quadratic(a=31, b=93, c=62)

来源:https://treyhunner.com/2018/04/keyword-arguments-in-python/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2018-05-27
    • 2021-01-03
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多