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.
Visualize data while debugging code (and I want to use Spyder too!).
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.
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.
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.
(Spyder maintainer here) Spyder 4.2.0, released on November 8/2020, supports the ability to work with interactive Matplotlib plots while debugging. That works out of the box, i.e. it doesn't require setting any special option.
For previous versions, the best solution is to use the pause(n)
command from Matplotlib (where n
is a number of seconds) after showing the plot on ipdb
. Here is an example:
from matplotlib.pyplot import imshow, pause
import numpy as np
x = np.random.rand(4,5)
imshow(x)
pause(1)