【问题标题】:How to thread Matplotlib in Python?如何在 Python 中线程化 Matplotlib?
【发布时间】:2015-08-29 05:50:10
【问题描述】:

当我线程 matplotlib 并且我不经常关闭图形窗口(通过鼠标 - 关闭窗口按钮)时,我收到以下错误:

Exception RuntimeError: RuntimeError('main thread is not in main loop',) in <bound method PhotoImage.__del__ of <Tkinter.PhotoImage instance at 0x7ff408080998>> ignored Exception RuntimeError: RuntimeError('main thread is not in main loop',) in <bound method PhotoImage.__del__ of <Tkinter.PhotoImage instance at 0x7ff3fbe373f8>> ignored
                                                                   Tcl_AsyncDelete: async handler deleted by the wrong thread
                                                                                                                             Aborted

我做什么:我有一个主循环调用实例的方法。 matplotlib 方法由该实例的 init 函数线程化。我不能调用matplotlib作为实例内部的方法,我不知道为什么,所以我通过__init__线程它:

def __init__(self):
....
thread = threading.Thread(target=self.drawcharts, args=())
        thread.daemon = False                            
        thread.start()

线程方法:

def drawcharts(self):

        global endthread

        ..do some math..

        try:

            plt.plot(k)
            plt.plot(x1)
            plt.plot(x2)
            plt.plot(x3)
            #plt.plot(v)
            plt.ylabel('some numbers')

            plt.show(block=True)
            self.drawcharts();
        except:
            self.drawcharts()


        if endthread==1:
            return


        return

全局变量endthread是我程序的退出函数触发的(键盘异常)。

我真的需要matplotlib 独立于程序的其余部分运行,因为它是一个curses 程序,我无法更改结构,以便matplotlib 可以在非阻塞模式下运行或其他方式。我还需要阻塞模式,因为需要绘图窗口的小部件。

我想一遍又一遍地遍历同一个 matplotlib 窗口 - 有时不关闭绘图窗口 - 会使 matplotlib 中的某些内容溢出。有一段时间它就像一个魅力,但随后不优雅地退出。

我能做些什么呢?感谢您的帮助!

【问题讨论】:

    标签: python multithreading matplotlib plot


    【解决方案1】:

    您应该查看有关使matplotlib.show() 非阻塞的问题和答案。

    比如这个:Plotting in a non-blocking way with Matplotlib

    这个答案可以用更简单的方式做你想做的事吗? https://stackoverflow.com/a/42674363/3898873

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      相关资源
      最近更新 更多