Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force Matplotlib to draw while in the ipdb debugger in Spyder (or any other debugger)?

Tags:

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.

like image 532
Peter D Avatar asked Aug 16 '11 15:08

Peter D


People also ask

Does matplotlib work in Spyder?

The %matplotlib only works in IPython, not in the "dedicated Python console". If you want to use some specific backend in IPython console inside Spyder you need to select it in the preferences.

How do I use Python debugger on Spyder?

Spyder's debugger is integrated with the Breakpoints pane, which lists the file, line, and condition (if any) of every breakpoint defined. To open it, select Debug ‣ List breakpoints, or press Ctrl - Shift - B ( Cmd - Shift - B on macOS).

What is IPDB in Spyder?

IPdb is the IPython debugger console. In Spyder 4.2. 0 or above it comes with code completion, syntax highlighting, history browsing of commands with the up/down arrows (separate from the IPython history), multi-line evaluation of code, and inline and interactive plots with Matplotlib. This is fixed now.


1 Answers

(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) 
like image 126
Carlos Cordoba Avatar answered Sep 24 '22 11:09

Carlos Cordoba