Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Jupyter showing charts twice?

I'm starting out with Jupiter to analyze some sales data. It's working, but every time I plot a chart, the chart shows up twice. The first two charts in my notebook are plotted from within a class, and for these it doesn't happen (note, Model here refers to a model of a product, not a predictive model):

class Model:
    ...
    def plot(self):
        self.weekly_sales.plot() # a pandas Series
        self.decomposed.plot() # result of seasonal_decompose on the weekly_sales

my_model = Model('model name', sales)
%matplotlib inline
my_model.plot()

enter image description here

All looks good. But then I execute the next three lines:

my_model.weekly_sales.autocorr()
from stats models.graphics.tsaplots import plot_acf
plot_acf(my_model.weekly_sales)

enter image description here

Every subsequent plot appears twice, including an ARMA model's fit and plot_pacf.

It's not a big deal. I'm getting the information I need, but it is a bit annoying. Why is it doing this?

Update: Going further along in the analysis, I printed a DataFrame in the same Jupyter cell as the ACF and PACF outputs, and it printed the frame and showed both the charts only once. I went back to the other cells with duplicate chart outputs, added a print() at the end of each one, and now each one appears only once.

like image 802
Chuck Avatar asked Dec 31 '22 12:12

Chuck


1 Answers

The issue/question is arround for quite a while already. See issue 1265. So far "This seems to be caused by the plot_acf function both plotting the graph AND returning the results which then causes IPython Notebook to plot the results again."

For the issue there are several ways to overcome the double plotting.

1) as you mentioned add print() after the acf_plot

2) assign the output e.g. output_plt = plot_acf(my_model.weekly_sales)

3) add a semicolon after the row plot_acf(my_model.weekly_sales);

4) If matplotlib is imported anyway execute a plt.show() in the same cell

5) Changing the %matplotlib properties. This is not recommended as it has effects on other plottings

like image 155
mobias17 Avatar answered Jan 14 '23 15:01

mobias17