【发布时间】:2013-10-29 15:43:24
【问题描述】:
我知道关于 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"
谢谢你的帮助
【问题讨论】:
-
在主线程上进行所有绘图。至少对于 QT,如果您尝试这样做,gui 不喜欢它。
标签: python multithreading matplotlib plot