小编Fab*_*ian的帖子

用线程中的matplotlib绘图

我知道在matplotlib和线程上有很多问题,而且pyplot也不是线程.然而,我无法找到关于这个特定问题的任何内容.我想要做的是:绘制一个数字并每秒更新一次.为此,我想创建一个线程,但到目前为止,我甚至无法从线程中获得真实的情节.此外,我坚持使用qt4,因此可能是其他后端表现不同.

这是一个非常简单的例子:创建了一个图plot_a_graph().从主程序调用时,这可以正常工作,但会延迟主代码的进一步执行.但是,从线程调用时,不会显示任何图形.

import matplotlib
matplotlib.use("qt4agg")
import matplotlib.pyplot as plt
import threading
import time

def plot_a_graph():
    f,a = plt.subplots(1)
    line = plt.plot(range(10))
    plt.show()
    print "plotted graph"    
    time.sleep(4)


testthread = threading.Thread(target=plot_a_graph)

plot_a_graph()      # this works fine, displays the graph and waits
print "that took some time"

testthread.start() # Thread starts, window is opened but no graph appears
print "already there"
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

python plot multithreading matplotlib

12
推荐指数
1
解决办法
1万
查看次数

标签 统计

matplotlib ×1

multithreading ×1

plot ×1

python ×1