2

I am using VS Code and python. What happens is that in a program like:

import matplotlib.pyplot as plt 
import numpy as np
x = np.linspace(1,10)
plt.plot(x, np.sin(x))
plt.show()
plt.plot(x, np.cos(x))
plt.show()
plt.plot(x, np.tan(x))
plt.show()

is that it will stop the exicution of the skript at line 5 and show the plot in a seperate window. It will only continiue to execute the program if I close the plot. Then it will continue until line 7 and stop there.

In other edidors I dont have this behaviour. Like in Spyder. There it will show the polt at the corresponding line, but it will continue the execution of the skript. Is there a way to make the same thing happen in VS Code?

A workaround would be to use plt.figure(). But I am forced to use templates were I can not change the plt.show()'s in the skript. So, I cant use this.

Akut Luna
  • 191
  • 2
  • 10
  • 2
    Does this answer your question? [Plotting in a non-blocking way with Matplotlib](https://stackoverflow.com/questions/28269157/plotting-in-a-non-blocking-way-with-matplotlib) – Jao Sep 25 '20 at 12:26
  • @Akut Luna How are things going? Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance. – Jill Cheng Sep 29 '20 at 07:01

2 Answers2

1

In VSCode, when running and debugging the code, the results will be executed in the same terminal and then output the results, so the above code is executed and output in sequence.

However, according to your description, it is recommended that you use the Jupyter notebook function in VSCode without changing your code. The results will be output in turn and then displayed together.

Use: "Ctrl+Shift+P", "Python: Create Blank New Jupyter Notebook", input the code.

Result:

enter image description here

Reference: Jupyter Notebooks in Visual Studio Code.

If this is not what you want, please describe it in detail and let me know.

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25
0

Are you looking for this? It turns on "interactive mode". Plotting no longer blocks.

matplotlib.pyplot.ion()

With that, behaviour feels similar to plotting in R, even while in debug mode.

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67