dar*_*dog 4 python matplotlib 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])
Run Code Online (Sandbox Code Playgroud)
这是图形代码:
fig = plt.figure(figsize=(12,8))
sm.graphics.plot_regress_exog(ols_test,'lastqu',fig=fig)
Run Code Online (Sandbox Code Playgroud)
我一个接一个地得到上述图形的两个副本?有任何想法吗?使用 SM dev 6.0 提到的笔记本显示了重复的图表。
更改绘图代码以将结果分配给 fig 解决了该问题:
fig = plt.figure(figsize=(12,8))
fig=sm.graphics.plot_regress_exog(ols_test,'lastqu',fig=fig)
Run Code Online (Sandbox Code Playgroud)
谢谢指点:
statsmodels 正在返回图形实例,ipython 在某些设置中坚持绘制两次 github.com/statsmodels/statsmodels/issues/1265 – user333700
| 归档时间: |
|
| 查看次数: |
1252 次 |
| 最近记录: |