Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i get statsmodels.api to plot time series plots just once?

I am having trouble plotting AR and MA models for time series of weather. For example:

from statsmodels.tsa.arima_process import arma_generate_sample

def gen_ar2(alpha1,alpha2,size):
    ar = [1,-alpha1,-alpha2]
    ma = [1]
    return arma_generate_sample(ar,ma,size)

Hence, if I want to plot lets say x(t) = 0.75 x(t-1) - 0.125 x(t-2) + w(t), where w(t) is Normal with mean 0 and variance 1 (lets say)...plotting this generally plots twice on ipython notebook, i.e

sm.graphics.tsa.plot_acf(gen_ar2(0.75,-0.125,size=5000),lags=40)

this creates two plots for me instead of just one. how do i stop ipython from doing this?

thanks

like image 314
turtle_in_mind Avatar asked Dec 12 '14 16:12

turtle_in_mind


Video Answer


1 Answers

You see two plots because the plot_acf function creates the plot and returns the figure object. In a "normal" python IDE or script this would not result in two images, but IPython with its inline backend displays a figure once it is created in a cell. Moreover, the returned figure is displayed as well. Later is indicated by the OUT[x] prompt.

To get a single image, simply save the output to some variable (to prevent its display) like

_ = sm.graphics.tsa.plot_acf(gen_ar2(0.75,-0.125,size=5000),lags=40)

or suppress the output like (note the ;!)

sm.graphics.tsa.plot_acf(gen_ar2(0.75,-0.125,size=5000),lags=40);

In both ways you only get the displayed data and not the returned data displayed.

like image 132
Jakob Avatar answered Nov 14 '22 21:11

Jakob