Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive(?) plotting in Spyder with matplotlib

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")
like image 480
Daniel Avatar asked May 23 '12 05:05

Daniel


People also ask

How to get interactive plots in Matplotlib?

To get interactive plots, we need to activate the figure. Using plt.ioff () and plt.ion (), we can perform interactive actions with plot. Create fig and ax variables using subplots method, where default nrows and ncols are 1.

How to visualize a Matplotlib plot in Jupyter?

Leveraging the Jupyter interactive widgets framework, IPYMPL enables the interactive features of matplotlib in the Jupyter notebook and in JupyterLab. To enable interactive visualization backend, you only need to use the Jupyter magic command: Now, let us visualize a matplotlib plot.

How to use % magic in matplotlib and IPython?

Using the % magic is guaranteed to work in all versions of Matplotlib and IPython. Enable interactive mode. Disable interactive mode. Return whether plots are updated after every plotting command. Display all open figures. Run the GUI event loop for interval seconds. whether changes to artists automatically trigger re-drawing existing figures

How do I restart Spyder to show plot?

You might not need to restart spyder. Just go to the tool menu and restart the kernel. Then you should have the plot displayed. Please, this is after you must have set the appropriate preferences for graphics Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question.


1 Answers

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.

like image 86
Daniel Avatar answered Sep 17 '22 17:09

Daniel