其他分享
首页 > 其他分享> > Statsmodels重复图表?

Statsmodels重复图表?

作者:互联网

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

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
参考的笔记本显示重复的图表.

解决方法:

更改绘图代码以将结果分配给无花果可解决此问题:

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

标签:matplotlib,python,statsmodels
来源: https://codeday.me/bug/20191122/2057130.html