kdemirtas
kdemirtas

Reputation: 111

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

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.

Upvotes: 11

Views: 35768

Answers (5)

H.B
H.B

Reputation: 1

I suggest you use only one plt.show() at the end of all plotting commands. So your code should look like this:

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

plt.plot([2,2,2,2,2]) 
plt.show()

Upvotes: 0

Simon Ldj
Simon Ldj

Reputation: 31

I can suggest you an Open source tool I been working on for similar reasons. I wanted to see plots one after the other while debugging, without dealing with closing windows or specific environment...

This answer is not helping you directly with Matplotlib but suggesting an alternative way to solve same issues. The problems I find with Matplotlib or IPython and similar tools are that they usually very restricted to certain environment (like in your case, the IDE). Also those tools usually hard to control to your own display requirements (or require much time).

HypnoLog can help you plot any data from Python or any other language. It will display the plots in a web browser, at the same page one after the other (like a log). As HypnoLog use the browser to display the plots, you can easily use any other tools avialble on the web to display plots or create your own.

Specifically for Python there is already a language wrapper, see HypnoLog-Python. And to plot numerical arrays you can use the built-in visualization plot.

This how it looks like in Python:

import hypnolog as HL
HL.log([1,2,3,4,5], 'plot');

Upvotes: 0

erik-e
erik-e

Reputation: 3891

I haven't done this in Visual Studio but with Matplotlib you can use a non-blocking call to show. The draw method does not block but if you don't end the calls by doing a show then the graph window will immediately close.

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

# Create a new figure to show the extra information.
# http://matplotlib.org/users/pyplot_tutorial.html#working-with-multiple-figures-and-axes
plt.figure(2)
plt.plot([2,2,2,2,2]) 
plt.show()

Here is a related question on the same topic.

Personally I use subplots as shown in the matplotlib examples.

Upvotes: 6

Pavel Minaev
Pavel Minaev

Reputation: 101555

Are you trying to get this work in your own script, or in the Python Interactive window?

If it's the latter, then you should enable IPython mode for it in Tools -> Options -> Python Tools -> Interactive Windows -> Interactive Mode (the default is "Standard"; change it to "IPython", and also make sure that you have the right Python selected in the "Show settings for" combo box if you have more than one installed). Then you won't need plt.show at all, as plt.plot will immediately display the plot inline directly inside the Interactive window.

Upvotes: 1

ljetibo
ljetibo

Reputation: 3094

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

Upvotes: 9

Related Questions