I know there are quite some questions on matplotlib and threading, also that pyplot is not threadsave. I couldn't find anything on this particular problem however. What I want to do is: plot a figure and update it every second. For this I wanted to create a thread, but so far I couldn't even get a real plot from the thread. Also, I'm stuck with qt4, so it may be other backends behave different.
Here is a very simple example: A plot is created in plot_a_graph()
. This works fine when called from the main program but delays the further execution of the main code. When called from a thread however, no graph is displayed.
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"
Thx for you Help
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…