修改统计模型图

Modifying a statsmodels graph

我在此处关注 statsmodels 文档: https://www.statsmodels.org/stable/vector_ar.html

我看到页面中间的部分说: irf.plot(orth=False)

为我的数据生成以下图表:

我需要修改图表的元素。例如,我需要应用 tight_layout 并减小 y-tick 大小,这样它们就不会进入左侧的图表。

文档讨论了将“子图绘图函数”传递给 irf.plot() 的 subplot 参数。但是当我尝试类似的东西时:

irf.plot(subplot_params = {'fontsize': 8, 'figsize' : (100, 100), 'tight_layout': True})

只有 fontsize 参数有效。我也尝试将这些参数传递给 'plot_params' 参数,但无济于事。

所以,我的问题是如何访问此 irf.plot 的其他参数,尤其是 figsize 和 ytick 大小?我还需要强制它打印一个网格,以及 x 轴上的所有值 (1, 2, 3, 4, ..., 10)

有什么方法可以使用 fig, ax = plt.subplots() 方法创建一个空白图,然后在该图上创建 irf.plot 吗?

看起来像函数 returns a matplotlib.figure:

尝试这样做:

fig = irf.plot(orth=False,..)
fig.tight_layout()
fig.set_figheight(100)
fig.set_figwidth(100)

如果我运行用这个例子,它会起作用:

import numpy as np
import pandas
import statsmodels.api as sm
from statsmodels.tsa.api import VAR

mdata = sm.datasets.macrodata.load_pandas().data
dates = mdata[['year', 'quarter']].astype(int).astype(str)
quarterly = dates["year"] + "Q" + dates["quarter"]
from statsmodels.tsa.base.datetools import dates_from_str
quarterly = dates_from_str(quarterly)
mdata = mdata[['realgdp','realcons','realinv']]
mdata.index = pandas.DatetimeIndex(quarterly)
data = np.log(mdata).diff().dropna()
model = VAR(data)

results = model.fit(maxlags=15, ic='aic')
irf = results.irf(10)

fig = irf.plot(orth=False)
fig.tight_layout()
fig.set_figheight(30)
fig.set_figwidth(30)