13

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")
Nick Craig-Wood
  • 52,955
  • 12
  • 126
  • 132
Daniel
  • 3,344
  • 5
  • 27
  • 35
  • 1
    What is the IPython version, and what's the Interpreter setting in the Run configurations when you press F6? It should be interactive if the version is IPython 0.10.2 and you choose "Execute in current Python or IPython interpreter". – HYRY May 23 '12 at 05:55
  • Wow, thank-you for such a rapid response, I was getting ready to go to bed =P. IPython version is 0.10.2.. pressing F6 showed the interpreter set to "Execute in a new dedicated python interpreter" .. switching it to "Execute in current Python or IPython interpreter" fixes the hanging problem I was having. – Daniel May 23 '12 at 06:07
  • @Daniel When you solved your problem you should answer your question and mark your answer as the right one. – bmu May 23 '12 at 15:43
  • Didn't realize, thanks, I'll do that. – Daniel May 23 '12 at 17:01

2 Answers2

17

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.

Daniel
  • 3,344
  • 5
  • 27
  • 35
2

in my case, these were the default settings in spyder however it still didn't show the plot until I typed: %matplotlib inline

Not sure if this is helpful but thought of sharing here.

Munish
  • 667
  • 3
  • 13
  • 34