Question

I have posted a IPython Notebook here http://nbviewer.ipython.org/gist/dartdog/9008026 in cell 5 I call: Edit this code should produce the duplicate graphs.

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])

This is the graph code:

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

And I get two copies of said graphics one after the next? Any ideas? Using SM dev 6.0 The notebook referred to shows the duplicate graphs.

Était-ce utile?

La solution

Changing the plotting code to assign the result to fig fixes the issue:

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

Thanks for the pointer:

statsmodels is returning the figure instance, ipython in some settings insists on plotting twice github.com/statsmodels/statsmodels/issues/1265 – user333700

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top