Search code examples
pythonmatplotlibvisual-studio-codejupyteripynb

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


Solution

  • 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.