Plotting with Matplotlib in Visual Studio using Python Tools for Visual Studio

kdemirtas_ picture kdemirtas_ · Feb 23, 2015 · Viewed 28.1k times · Source

I am new to using PTVS for my Python code. I previously used Spyder as it came along with the Anaconda distribution. Here is the issue I am having.

I am trying to create two plots, and show them in separate windows at the same time. A simple example is.

import matplotlib.pyplot as plt 
plt.plot([1,2,3,4,5]) 
plt.show() 
plt.plot([2,2,2,2,2]) 
plt.show()

But the second plot does not show up unless I close the first plot. Similarly for a larger script, the rest of the code chunk after plt.show() does not execute if I don't close the plot. I tried executing without debugging and it does not work. However, when I run the same code in Ipython interactive window, all the code executes and I can see both plots as inline, yet it is not what I want. I want to all the code to run creating as many plots as needed in different windows without me interrupting like closing the plot for the rest of the code to run. I can still do it in Spyder, but I want to switch to Visual Studio completely and this issue is bugging me.

Any help would be appreciated. Thanks in advance.

Answer

ljetibo picture ljetibo · Feb 23, 2015

Now I don't know how this would work in VS but whenever I have to make multiple plots in interactive window (without saving them) and I want to display all of them together this is what I do:

fig, ax = plt.subplots()
ax.plot([1,2,3,4,5])
fig1, ax1 = plt.subplots()
ax1.plot([1,2,3,4,5])
plt.show()

do notice that you will be stuck in place (still) after the plt.show(). To have an interactive window that doesn't obstruct the rest of your code, you have to use IPython, or some other tool which enables async plotting and calculations. However directly from Python interpreter, you're going to have to wait until you terminate that line.

Additionally you could turn on interactive mode by doing plt.ion() before anything else. This enables direct view to your plot, which updates after you issue commands pertaining to it.

I believe that is the actual command you're looking for. Here's the manual