Plotting with Matplotlib in Visual Studio using Python Tools for Visual Studio
Asked Answered
D

4

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.

Devon answered 23/2, 2015 at 16:44 Comment(0)
A
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

Annihilation answered 23/2, 2015 at 16:57 Comment(7)
Never knew about the #ion method, extremely useful. Thank you.Pfister
@Pfister 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.Annihilation
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.Devon
@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.Annihilation
@Annihilation 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.Devon
@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.Annihilation
If you're using ion, you should probably disable IPython mode for the REPL - that will turn off plot inlining etc.Peay
P
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.

Pfister answered 23/2, 2015 at 16:59 Comment(2)
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.Devon
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.Pfister
P
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.

Peay answered 23/2, 2015 at 17:50 Comment(2)
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.Devon
I would recommend using the subplot approach in this case, as described by another answer.Peay
H
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');
Hydrophane answered 4/11, 2018 at 21:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.