python - How to update a plot event driven? -


how can update matplotlib plot event driven?

background: wanna program tool displays me measured values receive via serial port. these values send serial connected device when has measured values , not when host requests.

current state: seems there no way update plot manually. when call plot()-function more 1 time new plots added. similar matlab or octave, when use "hold on"-option. ends after 10 calls. freezed. when clear figures before update, whole plot disappears. @ least, when plot embedded in window. neither draw() nor show() provide remedy.

when use single plot figure, there no easy way update, because after call of show(), program flow sticks @ line, until window closed. update has done in separate thread.

both problems solved, when use animation:

import sys pyqt4.qtgui import qapplication, qmainwindow, qdockwidget, qvboxlayout,qtabwidget, qwidget pyqt4.qtcore import qt matplotlib import pyplot, animation matplotlib.backends.backend_qt4agg import figurecanvasqtagg random import randint  = qapplication(sys.argv) w = qmainwindow() t = qtabwidget(w) tab1 = qwidget() t.addtab(tab1, '1st plot') t.resize(1280, 300)  x = [1, 2, 3] fig1 = pyplot.figure(); plot =  fig1.add_subplot(111); plot.plot(x) plot.grid();  layout = qvboxlayout(); layout.addwidget(figurecanvasqtagg(fig1)); tab1.setlayout(layout);  def updatefunction(i):     x.append(randint(0, 10));     plot.clear();     plot.plot(x);     plot.grid();  anim = animation.funcanimation(fig1, updatefunction, interval=500)  w.showmaximized() sys.exit(a.exec_()) 

but such animation time triggered. there solution update event triggered?

the event should finished uart read in.

thank much. fabian

@tcaswell

thank you!

this actual code:

import sys pyqt4.qtgui import qapplication, qmainwindow, qvboxlayout,qtabwidget, qwidget pyqt4.qtcore import signal matplotlib import pyplot matplotlib.backends.backend_qt4agg import figurecanvasqtagg random import randint import time threading import thread  = qapplication(sys.argv) w = qmainwindow() t = qtabwidget(w) tab1 = qwidget() t.addtab(tab1, '1st plot') t.resize(1280, 300)  x = []; x.append(randint(0, 10)); x.append(randint(0, 10)); x.append(randint(0, 10)); fig1 = pyplot.figure(); plot =  fig1.add_subplot(111); line, = plot.plot(x) plot.grid();  layout = qvboxlayout(); layout.addwidget(figurecanvasqtagg(fig1)); tab1.setlayout(layout);  def triggerupdate():     while true:         a.emit(signal('updateplot()'));         time.sleep(2);         print('update triggered!\n')  def updatefunction():     x.append(randint(0, 10));     #plot.clear();     line.set_data(range(len(x)), x);     fig1.canvas.draw_idle();     print('update done!\n')  a.connect(a, signal('updateplot()'), updatefunction)  t = thread(target=triggerupdate); t.start(); w.showmaximized()  sys.exit(a.exec_()) 

it seems run, content of plot gets not updated. plot has no canvas. (although should have one, otherwise don't on plotting when command it.)

to honest, did not understand concept of gui , plot foo. missing canvas covered parenting figure. don't got it. when there figure, can handle more 1 plots, should't each plot have own canvas?

but think, here problem.

thank much.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -