我在这里重新绘制数字时遇到问题.我允许用户在时间刻度(x轴)中指定单位,然后重新计算并调用此函数plots().我希望情节简单地更新,而不是在图中附加另一个情节.
def plots():
global vlgaBuffSorted
cntr()
result = collections.defaultdict(list)
for d in vlgaBuffSorted:
result[d['event']].append(d)
result_list = result.values()
f = Figure()
graph1 = f.add_subplot(211)
graph2 = f.add_subplot(212,sharex=graph1)
for item in result_list:
tL = []
vgsL = []
vdsL = []
isubL = []
for dict in item:
tL.append(dict['time'])
vgsL.append(dict['vgs'])
vdsL.append(dict['vds'])
isubL.append(dict['isub'])
graph1.plot(tL,vdsL,'bo',label='a')
graph1.plot(tL,vgsL,'rp',label='b')
graph2.plot(tL,isubL,'b-',label='c')
plotCanvas = FigureCanvasTkAgg(f, pltFrame)
toolbar = NavigationToolbar2TkAgg(plotCanvas, pltFrame)
toolbar.pack(side=BOTTOM)
plotCanvas.get_tk_widget().pack(side=TOP)
Run Code Online (Sandbox Code Playgroud) 我知道在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)
谢谢你的帮助
我正在一个线程中从套接字读取数据,并希望在新数据到达时绘制和更新绘图.我编写了一个小型原型来模拟事物,但它不起作用:
import pylab
import time
import threading
import random
data = []
# This just simulates reading from a socket.
def data_listener():
while True:
time.sleep(1)
data.append(random.random())
if __name__ == '__main__':
thread = threading.Thread(target=data_listener)
thread.daemon = True
thread.start()
pylab.figure()
while True:
time.sleep(1)
pylab.plot(data)
pylab.show() # This blocks :(
Run Code Online (Sandbox Code Playgroud)