我想修改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))
为了结束这个问题,我从评论移到回答。
您的代码的问题是,您使用带点的关键字参数-这是无效的语法。但是您根本不需要使用关键字参数。相反,您需要调用以下命令:
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/