【问题标题】:python statsmodels ARMA plot_predictpython statsmodels ARMA plot_predict
【发布时间】:2015-02-12 20:47:42
【问题描述】:

我正在使用 statsmodels 来计算带有预测的 ARMA 模型。我想更改趋势的颜色,但出现错误:

fig = arma_mod30.plot_predict('2011', '2015', color='#FF6600', dynamic=True, ax=ax, plot_insample=False) 类型错误:plot_predict() 得到了一个意外的关键字参数“颜色”

绘图代码:

 fig, ax = plt.subplots(figsize=(12, 8))
 ax = d.ix['2009':].plot(ax=ax,label='Trend',color='#0000FF')
 fig = arma_mod30.plot_predict('2011', '2018', color='#FF6600',  dynamic=True, ax=ax, plot_insample=False)
 plt.title('Forecast Trend')
 plt.xlabel('year')
 plt.ylabel('value')
 plt.savefig('Output.png')

【问题讨论】:

  • 看来这是不可能的。 plot_predict 似乎没有将其他关键字参数传递给绘图例程。
  • 在你的绘图之前设置mpl.rc('axes', color_cycle=['#0000FF', '#FF6600'])是一个丑陋的黑客,但它可能应该可以工作。

标签: python statsmodels


【解决方案1】:

本示例基于statsmodels'文档中plot_predict的示例代码:

这里我使用mpl.rc_context()来临时改变图形的颜色循环。

with mpl.rc_context():
    mpl.rc('axes', color_cycle=['#0000FF', '#FF6600'])
    dta = sm.datasets.sunspots.load_pandas().data[['SUNACTIVITY']]
    dta.index = pd.DatetimeIndex(start='1700', end='2009', freq='A')
    res = sm.tsa.ARMA(dta, (3, 0)).fit()
    fig, ax = plt.subplots()
    ax = dta.ix['1950':].plot(ax=ax)
    fig = res.plot_predict('1990', '2012', dynamic=True, ax=ax,
                           plot_insample=False)

这可能有点老套,但应该可以解决您的问题:

【讨论】:

  • 我认为这根本不是 hackish。这就是 matplotlib 的全部优点。您还可以使用返回的图形对象更改颜色。
  • 新的bug是AttributeError: 'ARMAResults' object has no attribute 'plot_predict'.
  • @AlexanderYau,该代码触发了弃用警告,但仍适用于最新稳定版本的 statsmodels (0.6.1)。
猜你喜欢
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
  • 2013-09-08
  • 2016-03-04
  • 1970-01-01
  • 2014-06-14
  • 2013-05-25
  • 2015-04-20
相关资源
最近更新 更多