Akut Luna
Akut Luna

Reputation: 359

dont stop at plt.show() in VS Code

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.

Upvotes: 2

Views: 5054

Answers (2)

FXQuantTrader
FXQuantTrader

Reputation: 6891

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.

Upvotes: 1

Jill Cheng
Jill Cheng

Reputation: 10354

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.

Upvotes: 1

Related Questions