【问题标题】:Spyder interactive plot : wait for the plot to be closed to continueSpyder 交互式绘图:等待绘图关闭以继续
【发布时间】:2018-03-13 17:38:46
【问题描述】:

我正在使用 Spyder 处理 Windows,我使用 matplotlib 进行绘图。我的问题是我想做交互式绘图(或者有时绘制很多东西)并且我希望 spyder 等待我关闭图形以继续代码(与传统终端相同的方式)。

我试过了 plt.ion(), %mpl TkAgg 在加载 matplotlib、Ipython 和 python 控制台之前......我找不到任何解决方案。

如果你想要一个例子,目标是“你好”只在我关闭图形时打印,在 Windows 10 上使用 Spyder。

import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()

print("hello")

【问题讨论】:

    标签: matplotlib spyder interactive


    【解决方案1】:

    您需要通过以下方式停用 Spyder 的 Matplotlib 支持

    Tools > Preferences > IPython console > Graphics

    并取消选择调用的选项

    Activate support

    那么你需要像这样改变你的代码

    import matplotlib
    matplotlib.use('TkAgg')
    import matplotlib.pyplot as plt
    plt.figure('Close me to get hello')
    plt.plot(0,0,'*')
    plt.show()
    
    print("hello")
    

    在创建情节之前手动设置您的后端(在本例中为TkAgg)。

    【讨论】:

      【解决方案2】:

      当我运行代码时,阻止后续代码执行的绘图窗口的期望行为已经存在。所以我想还有一些其他的设置。出于这个原因,我也无法测试以下内容,但我认为您需要在调用 show 之前关闭交互模式。

      import matplotlib.pyplot as plt
      plt.ion() # turn interactive on, if it isn't already
      plt.figure('Close me to get hello')
      plt.plot(0,0,'*')
      plt.draw()
      # possibly do other stuff that would make the use of interactive mode useful
      
      plt.ioff() # turn interactive off
      plt.show()
      
      print("hello")
      

      【讨论】:

        猜你喜欢
        • 2017-04-17
        • 2021-07-07
        • 2017-05-26
        • 2019-08-17
        • 2012-05-29
        • 2018-10-13
        • 2017-11-09
        • 2017-02-14
        • 1970-01-01
        相关资源
        最近更新 更多