Interactive(?) plotting in Spyder with matplotlib

Daniel picture Daniel · May 23, 2012 · Viewed 29.7k times · Source

I am trying to migrate over to Python from Matlab and can't figure out how to get interactive(?) plotting working within the Spyder IDE. My test code is shown below. With the .ion() nothing happens, I get a quick flash of a figure being drawn then the window instantly closes and spits out my Hello. Without the .ion() the figure is drawn correctly but the script hangs and doesn't spit out Hello until I manually close the figure window. I would like the script to run like a matlab script would and plot the various figures I ask it to while chugging along any computations and putting the output on the terminal(?) window.

I tried typing out the lines one at a time in ipython and it seemed to work but I would much rather work in a script sheet format where I can go back and forth between lines tweaking code.

I'm working in windows 7 if that helps. I installed python(x,y) and am launching spyder from there (spyder version 2.1.9). I have seen some similar-ish questions asked but I wasn't able to solve this problem. It seemed to me that someone said ipythons latest version is not compatible with spyder but then I saw another post that said interactive plotting should be supported regardless. Thanks for the help! If anyone has alternative environments I could use to mimick matlab behaviour that would work too, I'm really new to Python.

import matplotlib.pylab as plt
plt.ion()
plt.plot([1,2,3])
plt.show()
plt.ylabel('This is an axis')
print ("Hello")

Answer

Daniel picture Daniel · May 23, 2012

The run configuration should be set to Execute in current Python or IPython interpreter which by default allows for interactive plotting. If the interpreter is set to Execute in a new dedicated Python interpreter then Interact with the Python interpreter after execution must be selected.