将 plot_pacf 作为子图之一调用时更改子图轴范围

Changing subplot axes range when calling plot_pacf as one of the suplots

我无法为我正在制作的某些子图设置不同的 x 轴范围。据我所知,这可能是因为调用了 plot_pacf() 函数而不是使用 axes2.plot()

密码是:

# PACF plot of 1st differenced series
plt.rcParams.update({'figure.figsize':(9,3), 'figure.dpi':120})

# I do not know why the data is defaulting to starting from 1970..but this is my attempt to fix it
x_axis_min = datetime.datetime(2013, 1, 1)
x_axis_max = datetime.datetime(2015, 6, 30)
x_axis_min_pacf = datetime.datetime(1969, 12, 31)
x_axis_max_pacf = datetime.datetime(1970, 2, 1)

fig, (axes1, axes2) = plt.subplots(1, 2, sharex=True)

# differencing plot
axes1.plot(store1_train.Sales.diff())
axes1.set_title('1st Differencing')
axes1.set_xlim(x_axis_min, x_axis_max)
axes1.tick_params(axis='x', labelrotation=90)

# pacf plot
plot_pacf(store1_train.Sales.diff().dropna(), ax=axes2)
axes2.set_xlim(x_axis_min_pacf, x_axis_max_pacf)
axes2.set_ylim((0,1.1))
axes2.tick_params(axis='x', labelrotation=90)

plt.show()

但是输出没有为每个子图设置特定的 x 轴范围。相反,它只需要 x_axis_min_pacfx_axis_max_pacf 作为两个子图的范围

(注意:我知道 x 轴日期范围不会使 sense/arent' 在 axes1axes2 之间具有可比性。这是一个单独的问题导致了这个问题的问题)

首先,sharex=True 和你的另一个 post 是同一个问题,这就是为什么两者都需要 x_axis_min_pacf 和 'x_axis_max_pacf'地块:

其次,将 x 轴上的刻度设置为日期实际上没有意义,因为 pacf 相关值与日期无关。相关性适用于数据集中的每个观察结果,实际上与日期没有任何关系。

因此,我建议删除行 axes2.set_xlim(x_axis_min_pacf, x_axis_max_pacf) 并改用数值(表示滞后数)。但是,您可以在 pacf 图中绘制一些线条以增加可读性。在 plot_pacf(…):

之前添加这样的内容
number_of_lags=30
steps_with_lines=7
for x in range(0, number_of_lags+1, steps_with_lines):

    plt.axvline(x=x, ymin=0, ymax=1, color="lightblue", linestyle="--")