7

I used to work in Matlab and it is really convenient (when working with big arrays/matrices and nested functions) to visualize intermediate results during debugging using plot function.

In Python I cannot plot anything in debug mode: a window with figure plot is never loaded (I am using Spyder IDE for coding and matplotlib.pyplot for plotting).

This is really annoying when debugging nested function and classes. Does anyone know a good solution? Of course, I can always output intermediate results, however it is not convenient.

Thanks, Mikhail

Mikhail Genkin
  • 3,247
  • 4
  • 27
  • 47
  • 1
    Do you know Jupyter notebook: https://jupyter.org/? – xdze2 Jul 24 '18 at 19:47
  • 1
    I don't know any solution. But I want to mention that I never ever used Debugging mode even when debugging. Instead, `print` statements are completely fine to find out the current state of a script. – ImportanceOfBeingErnest Jul 24 '18 at 19:47
  • Thanks for suggestions. I did use Jupyter notebook, but I'd prefer spyder. @ImportanceOfBeingErnest how do you use print statements? In command line during debug mode? When working with big (say, N=1000) vectors, visualization is more convenient – Mikhail Genkin Jul 24 '18 at 20:10
  • 1
    What I mean is that when not in Debugging mode, you can easily plot whatever you like at any point in the script, just as you'd do with print statements. – ImportanceOfBeingErnest Jul 24 '18 at 20:50
  • @ImportanceOfBeingErnest but how do I execute my script up to a certain point without a debugger? – Mikhail Genkin Jul 24 '18 at 20:58
  • 1
    If you call `plt.show()` the script stops automatically until you close the figure. You can also trigger any error, which lets the script stop, like `assert False`. – ImportanceOfBeingErnest Jul 24 '18 at 21:13
  • That is a good alternative. Thanks. It answers my quesiton – Mikhail Genkin Jul 24 '18 at 21:50

1 Answers1

9

Ok, I found a way to show the plot without breaking the debugging process.

All you need to do is to issue plt.pause(1) command, which will display the plots, and then one can continue the debugging process.

Mikhail Genkin
  • 3,247
  • 4
  • 27
  • 47