Python 我的子地块高度在下降。有没有办法保持它们不变
我的子地块高度在下降。有没有办法保持它们不变。每行的高度应保持不变Python 我的子地块高度在下降。有没有办法保持它们不变,python,matplotlib,Python,Matplotlib,我的子地块高度在下降。有没有办法保持它们不变。每行的高度应保持不变 from statsmodels.graphics.tsaplots import plot_pacf from statsmodels.graphics.tsaplots import plot_acf for index,store in enumerate(StoreData): print('\033[1m PACF AND ACF for Store:', store, '\033[0m') lag_ac
from statsmodels.graphics.tsaplots import plot_pacf
from statsmodels.graphics.tsaplots import plot_acf
for index,store in enumerate(StoreData):
print('\033[1m PACF AND ACF for Store:', store, '\033[0m')
lag_acf = acf(StoreData[store]['Sales'], nlags=20) # MA
lag_pacf = pacf(StoreData[store]['Sales'], nlags=20, method='ols') # AR
#Plot PACF
plt.subplot(str(index+1),2,1)
plt.plot(lag_pacf)
plt.axhline(y=0, linestyle='--', color='gray')
plt.axhline(y=-1.96/np.sqrt(len(StoreData[store]['Sales'])), linestyle='--', color='gray')
plt.axhline(y=1.96/np.sqrt(len(StoreData[store]['Sales'])), linestyle='--', color='gray')
plt.title('Partial Autocorrelation Function')
#Plot ACF:
plt.subplot(str(index+1),2,2)
plt.plot(lag_acf)
plt.axhline(y=0, linestyle='--', color='gray')
plt.axhline(y=-1.96/np.sqrt(len(StoreData[store]['Sales'])), linestyle='--', color='gray')
plt.axhline(y=1.96/np.sqrt(len(StoreData[store]['Sales'])), linestyle='--', color='gray')
plt.title('Autocorrelation Function')
# plt.tight_layout()
plt.show()
plt.subplot的调用签名是
subplot(nrows、ncols、index、**kwargs)
()
您应该通过将nrows
设置为存储数量并计算正确的索引来相应地修复代码:
n_stores = len(StoreData)
for index,store in enumerate(StoreData):
# ...
plt.subplot(n_stores, 2, 2*index+1) # +1 for PACF, +2 for ACF
# ...
每次执行命令
plt.subplot
时,您的图形都会被分割。您是否尝试过先创建布局,然后使用plt.subplot
(附加)选择要打印的右轴
嗨,谢谢!这种方法奏效了。但不是我所有的打印声明都在子部分之上。有没有办法先打印一条语句,然后再打印一个子图,用2种颜色等等?你太棒了!上述解决方案在回路中使用plt.子批次(figsize=(15,50))有效。谢谢
from statsmodels.graphics.tsaplots import plot_pacf
from statsmodels.graphics.tsaplots import plot_acf
fig, axs = plt.subplots(len(StoreData), 2)
for index,store in enumerate(StoreData):
print('\033[1m PACF AND ACF for Store:', store, '\033[0m')
lag_acf = acf(StoreData[store]['Sales'], nlags=20) # MA
lag_pacf = pacf(StoreData[store]['Sales'], nlags=20, method='ols') # AR
#Plot PACF
plt.sca(axs[index][0])
plt.plot(lag_pacf)
plt.axhline(y=0, linestyle='--', color='gray')
plt.axhline(y=-1.96/np.sqrt(len(StoreData[store]['Sales'])), linestyle='--', color='gray')
plt.axhline(y=1.96/np.sqrt(len(StoreData[store]['Sales'])), linestyle='--', color='gray')
plt.title('Partial Autocorrelation Function')
#Plot ACF:
plt.sca(axs[index][1])
plt.plot(lag_acf)
plt.axhline(y=0, linestyle='--', color='gray')
plt.axhline(y=-1.96/np.sqrt(len(StoreData[store]['Sales'])), linestyle='--', color='gray')
plt.axhline(y=1.96/np.sqrt(len(StoreData[store]['Sales'])), linestyle='--', color='gray')
plt.title('Autocorrelation Function')