如何在Python中使用Matplotlib?

Pet*_*eix 6 python plot multithreading matplotlib

当我线程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
Run Code Online (Sandbox Code Playgroud)

我做什么:我有一个主循环调用实例的方法.该matplotlib方法由该实例的init函数进行线程化.我不能matplotlib在实例中调用方法,我不知道为什么,所以我通过__init__以下方式调用它:

def __init__(self):
....
thread = threading.Thread(target=self.drawcharts, args=())
        thread.daemon = False                            
        thread.start()
Run Code Online (Sandbox Code Playgroud)

线程方法:

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
Run Code Online (Sandbox Code Playgroud)

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

我真的需要matplotlib独立于程序的其余部分运行,因为它是一个curses程序而且我无法更改结构,因此matplotlib可以在非阻塞模式下运行.此外,我需要阻止模式,因为需要绘图窗口的小部件.

我想反复遍历相同的matplotlib窗口 - 有时不关闭绘图窗口 - 在matplotlib中使某些东西溢出.有一段时间它的作用就像一个魅力,然后不合时宜地退出.

我该怎么办?谢谢你的帮助!

ala*_*tti -1

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

例如,这个:使用 Matplotlib 以非阻塞方式绘图

这个答案可以用更简单的方式做你想做的事吗?/sf/answers/2987205441/