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.
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.
2 Answers
Reset to default 1In 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.
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.