Willytom Rigaud
Willytom Rigaud

Reputation: 3

VSCode ipynb notebook showing the plots several times over

Does anyone know why my code does this in VSCode ? If I was using the jupyter notebook on a local host, I'm pretty sure it would not happen. Does anyone know how to fix that ?

if i write only ret_test.plot(), nothing shows up. I then write plt.show() and two to 5 plots show up. If I only write plt.show(), nothing happens.image of the output, there are 4 more graphs of the same plot

Upvotes: 0

Views: 33

Answers (2)

Coco Q.
Coco Q.

Reputation: 814

In VSCode's Jupyter implementation, plots can sometimes stack up and display multiple times. This is because each cell execution might be creating a new figure without clearing the previous ones

You can close all figures before plotting

plt.close('all')  # close all figures
ret_test.plot()
plt.show()

Also it is recommended to use %matplotlib inline at the beginning of your notebook. This ensures plots display properly without needing multiple plt.show() calls.

Upvotes: 1

Naveed Ahmed
Naveed Ahmed

Reputation: 473

If you explicitly call plt.show() and the plot object is also the last line in the cell, Jupyter would display the plot twice. To suppress this behavior of displaying automatic output, simply add ; at the end of the last line.

Upvotes: 0

Related Questions