问题
EDIT
Unfortunately, at the moment this is not possible. I found out that it is a bug in Spyder. The developers are still figuring out how to approach this.
Goal
Visualize data while debugging code (and I want to use Spyder too!).
Attempt #1: Run foo.bar from IPython from Spyder
Create a file named foo.py with the following code:
from ipdb import set_trace as st import matplotlib.pyplot as plt def bar(): st()
While in IPython, type the following:
In [4]: import foo In [5]: foo.bar() --Return-- None > somewhere_over_the_rainbow\foo.py(5)bar() 3 4 def bar(): ----> 5 st() ipdb> plt.plot([1, 2], [3, 4]) [<matplotlib.lines.Line2D object at 0x05CA8E90>] ipdb> plt.show()
Plot remains in "frozen" state. If I exit debugger, plot updates. If I try to close the plot, IPython crashes. Obviously both undesirable, and neither lets me see the data while debugging.
Attempt #2: Run foo.bar from IPython from command line
- Use same foo.py as in Attempt #1:
Open IPython from commandline:
In [4]: import foo In [5]: foo.bar() --Return-- None > somewhere_over_the_rainbow\foo.py(5)bar() 3 4 def bar(): ----> 5 st() ipdb> plt.plot([1, 2], [3, 4]) [<matplotlib.lines.Line2D object at 0x03904070>] ipdb> plt.show()
Program shows plot as I expect. BUT I want to use Spyder.
Attempt #3: Run baz.bar from IPython from command line
Write baz.py:
from ipdb import set_trace as st import matplotlib.pyplot as plt st()
Open IPython from commandline:
In [4]: import baz --Return-- None > somewhere_over_the_rainbow\baz.py(4)<module>() 2 import matplotlib.pyplot as plt 3 ----> 4 st() ipdb> plt.
Then Spyder fully freezes.
Any suggestions?
Note #1: In my full code, I have many files and many functions, so mashing it all together in one script without functions is not viable.
Note #2: Using any matplotlib interactive command (e.g. ion(), interactive(True), etc.) had no effect.
Note #3: Spyder version 2.0.12, Python 2.6, matplotlib 1.0.1.
回答1:
(Spyder dev here) Note: Sorry for answering this one after so much time, but perhaps someone else will find it uself.
The best solution right now (November/13) is to use the pause(n)
command from matplotlib
(where n
is a number of seconds) after showing the plot on pdb
. Here is an example:
from matplotlib.pyplot import imshow, pause
import numpy as np
x = np.random.rand(4,5)
imshow(x)
pause(1)
Check out this comment from Jed Ludlow, one of our former devs, where he describes this solution.
回答2:
Have you considered the ion()
function when importing pylab
? This should allow interactive plotting in pdb
.
import pylab
import pdb
pylab.ion()
tst_xdata = [1,2,3,4,5,6]
tst_ydata = [1,1,1,1,1,1]
pylab.plot(tst_xdata,tst_ydata)
pylab.draw()
pdb.set_trace()
for idx in range(3):
tst_ydata = [elem+2 for elem in tst_ydata]
pylab.plot(tst_xdata,tst_ydata)
pylab.draw()
pylab.show()
The above works on my machine (Ubuntu 11.04, Python 2.7, SciPy bersion 0.8.0), even running in Eclipse with PyDev.
回答3:
I found that you can actually plot in debug mode using Spyder now. It is surprisingly simple.
ipdb>pylab.plot(x,y)
ipdb>pylab.show()
......
来源:https://stackoverflow.com/questions/7080921/how-do-i-force-matplotlib-to-draw-while-in-the-ipdb-debugger-in-spyder-or-any-o