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.

kdemirtas
  • 111
  • 1
  • 1
  • 4

4 Answers4

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

ljetibo
  • 3,048
  • 19
  • 25
  • 1
    Never knew about the `#ion` method, extremely useful. Thank you. – erik-e Feb 23 '15 at 17:01
  • @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. – ljetibo Feb 23 '15 at 17:02
  • 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 Feb 23 '15 at 19:10
  • @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. – ljetibo Feb 23 '15 at 20:20
  • @ljetibo it is IPython Python REPL in Visual Studio. When I include plt.ion() at the beginning of my script, Python and Visual Studio crashes if I try to execute the script in the IPython interactive window. I guess the problem is Visual Studio specific, because I don't have any problem using Spyder for this purpose. I wanted to switch to Visual Studio, but I will stick with Spyder anyways. Thanks for the answers. – kdemirtas Feb 23 '15 at 20:45
  • @kdemirtas_ ah, yes. That is most likely your issue then. IPython REPL was most probably designed to work the same as `%pylab` magic the standard IPython Notebooks does. That basically does an `from pylab import *` (and then some more, like intercepting calls and inlining graphs etc..) and was not designed to be used the way you want to. If you start "normal" python ide `plt.ion()` should work more as expected. I would recommend wetting your toes with a virtualenv, I do believe that will allow you to extract the most from IPy (since both examples work in my win7 venv). Best of luck. – ljetibo Feb 23 '15 at 21:55
  • 1
    If you're using ion, you should probably disable IPython mode for the REPL - that will turn off plot inlining etc. – Pavel Minaev Feb 23 '15 at 22:19
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.

Community
  • 1
  • 1
erik-e
  • 3,721
  • 1
  • 21
  • 19
  • 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. – kdemirtas Feb 23 '15 at 19:11
  • 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. – erik-e Feb 23 '15 at 19:26
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.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • 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. – kdemirtas Feb 23 '15 at 19:07
  • I would recommend using the subplot approach in this case, as described by another answer. – Pavel Minaev Feb 23 '15 at 22:16
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');
Simon Ldj
  • 31
  • 2