4

我在单元格 5 中发布了一个 IPython Notebook http://nbviewer.ipython.org/gist/dartdog/9008026 我调用:编辑此代码应该产生重复的图形。

import pandas as pd
import io
import statsmodels.api as sm
%matplotlib inline
import matplotlib.pyplot as plt
content2 = '''\
    Units   lastqu
2000-12-31   19391   NaN
2001-12-31   35068   5925
2002-12-31   39279   8063
2003-12-31   47517   9473
2004-12-31   51439   11226
2005-12-31   59674   11667
2006-12-31   58664   14016
2007-12-31   55698   13186
2008-12-31   42235   11343
2009-12-31   40478   7867
2010-12-31   38722   8114
2011-12-31   36965   8361
2012-12-31   39132   8608
2013-12-31   43160   9016
2014-12-31   NaN     9785
'''
df2 = pd.read_table(io.BytesIO(content2))
#make sure that the columns are int
df2['Units']=df2['Units'][:-1].astype('int')
df2['lastqu']=df2['lastqu'][1:].astype('int')
df2=df2[['Units','lastqu']]
def fit_line2(x, y):
    X = sm.add_constant(x, prepend=True) #Add a column of ones to allow the calculation of the intercept
    ols_test = sm.OLS(y, X,missing='drop').fit()
    """Return slope, intercept of best fit line."""
    X = sm.add_constant(x)
    return ols_test
ols_test=fit_line2(df2['lastqu'][1:-1], df2['Units'][1:-1])

这是图形代码:

fig = plt.figure(figsize=(12,8))
sm.graphics.plot_regress_exog(ols_test,'lastqu',fig=fig) 

然后我得到两份上述图形的副本,一个接一个?有任何想法吗?使用 SM dev 6.0 提到的笔记本显示了重复的图表。

4

1 回答 1

5

更改绘图代码以将结果分配给 fig 解决了这个问题:

fig = plt.figure(figsize=(12,8))
fig=sm.graphics.plot_regress_exog(ols_test,'lastqu',fig=fig)

感谢指点:

statsmodels 正在返回图形实例,ipython 在某些设置中坚持绘制两次 github.com/statsmodels/statsmodels/issues/1265 – user333700

于 2014-02-14T23:21:03.837 回答