11

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.

5 Answers 5

9

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

Sign up to request clarification or add additional context in comments.

7 Comments

Never knew about the #ion method, extremely useful. Thank you.
@erik-e yes it is. However it's a bit tricky because sometimes the plot doesn't update properly. I haven't used it in a while though, maybe it's better now.
Thansk for the reply. The interactive window that I am using is already Ipyhton. I don't have a problem with inline plots, my problem occurs when I want to display the plots on separate windows rather than inline. The rest of the code does not execute before I close the first plot window that pops up.
@kdemirtas_ Is it IPython or IPython Notebook? Because those two aren't necessarily the same. If you used IPython Notebook magic this might not work as you imagined. If you haven't inlined anything on that Notebook, this should still work as expected. Post a more detailed question including minimal working example if you need more detailed help.
If you're using ion, you should probably disable IPython mode for the REPL - that will turn off plot inlining etc.
|
6

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.

2 Comments

Hello @erik-e, unfortunately when I use the draw method, both plots are displayed on the same figure as two lines with different colors. I want to have one window for each plot.
Ah, sorry I misunderstood. I will update my answer to display two figures which will open in separate windows. The updated answer may not work on all versions of python/matplotlib.
1

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.

2 Comments

Thanks for the answer, Pavel. I am trying to do it in my own script. Your reply works, however it is not what I want. I don't want the plot to be displayed inline. I want it to be displayed as a separate window.
I would recommend using the subplot approach in this case, as described by another answer.
0

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');

Comments

0

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()

2 Comments

Did you try this? It will put both lines on one figure, which is not what OP was looking for. They wanted two separate windows. Also note that the question is almost 10 years old and was specific to an IDE (PTVS) which is no longer developed. I suggest that there is little value in trying to solve such a question now.
Noted. In response to your question, yes it worked for me on VS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.