在Spyder(或任何其他调试器)的ipdb调试器中,如何强制Matplotlib绘制? [英] How do I force Matplotlib to draw while in the ipdb debugger in Spyder (or any other debugger)?

查看:243
本文介绍了在Spyder(或任何其他调试器)的ipdb调试器中,如何强制Matplotlib绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑



不幸的是,目前这是不可能的。我发现它是一个 Spyder中的错误。开发人员仍然在弄清楚如何处理这一点






目标



调试代码时可视化数据(我想要



尝试#1:从Spyder的IPython运行foo.bar




  • 使用以下代码创建一个名为foo.py的文件:

     从ipdb import set_trace as st 
    import matplotlib.pyplot as plt

    def bar():
    st()


  • 在IPython中键入以下内容:

     在[4]中: import foo 

    在[5]中:foo.bar()
    - 返回 -

    > somehere_over_the_rainbow\foo.py(5)bar()
    3
    4 def bar():
    ----> 5 st()

    ipdb> plt.plot([1,2],[3,4])
    [< matplotlib.lines.Line2D对象在0x05CA8E90>]
    ipdb> plt.show()




冻结状态。如果我退出调试器,绘图更新。如果我试图关闭情节,IPython会崩溃。显然,这两个都是不合需要的,也不允许我在调试时看到这些数据。



尝试#2:从命令行的IPython运行foo.bar




  • 使用与尝试#1相同的foo.py:

  • 从命令行打开IPython:

     在[4]中:import foo 

    在[5]中:foo.bar()
    - 返回 -

    > somehere_over_the_rainbow\foo.py(5)bar()
    3
    4 def bar():
    ----> 5 st()

    ipdb> plt.plot([1,2],[3,4])
    [< matplotlib.lines.Line2D对象在0x03904070>]
    ipdb> plt.show()




我预计。但是我想使用Spyder。



尝试#3:从命令行运行IPython的baz.bar




  • 将baz.py:

     从ipdb导入set_trace作为st 
    导入matplotlib.pyplot as plt

    st()


  • 打开IPython从命令行:

     在[4]中:import baz 
    - 返回 -

    > somehere_over_the_rainbow\baz.py(4)< module>()
    2 import matplotlib.pyplot as plt
    3
    ----> 4 st()

    ipdb> plt。




然后Spyder完全冻结。

任何建议?



Note#1 :在我的完整代码中,我有很多文件许多功能,所以在一个没有功能的脚本中将它们整合在一起是不可行的。



Note#2 :使用任何matplotlib交互式命令ion(),interactive(True)等)没有任何效果。



注3#:Spyder 2.0.12版,Python 2.6 ,matplotlib 1.0.1。

解决方案

Spyder dev here 注意:对不起,经过这么多时间回答这个问题,但也许别人会自己找到它。



现在(11月/ 13日)的最佳解决方案是使用暂停(n)命令从 matplotlib (其中 n 是显示 pdb 之后的情节的秒数)。以下是一个例子:

  from matplotlib.pyplot import imshow,pause 
import numpy as np
x = np.random.rand(4,5)
imshow(x)
pause(1)

查看这个评论从Jed Ludlow,我们之前的开发商之一,他在这里描述了这个解决方案。


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.

解决方案

(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.

这篇关于在Spyder(或任何其他调试器)的ipdb调试器中,如何强制Matplotlib绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆