Multithreading with updating plot

1,438 views
Skip to first unread message

Quinty van Dijk

unread,
Jun 1, 2017, 6:34:42 AM6/1/17
to pyqtgraph
Hi,

I'm trying to plot data from a drone in an updating scrolling plot. For the plotting I use the example code number 3 from this file:

Now i want to be able to send commands to the drone while the plots keep updating, because that's the data that I want to see.
Now I figured I need to multi thread using Qt, and got my code form here: https://groups.google.com/forum/#!topic/pyqtgraph/haiJsGhxTaQ

But when I start the thread the main thread sleeps. I'm new to this and have no idea what's happening, can someone help me?
Without the the threading the plots work just fine.

This is my code:

import pyqtgraph as pg
import time
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
from pyardrone import ARDrone, at
import thread1

## Code for plot, copied form example.
--


## Threading and updating part

def update():
    print("update")
    update_plot_vx()
    update_plot_vy()
    update_plot_vz()


class Thread(pg.QtCore.QThread):
    newData = pg.QtCore.Signal(object)
    def run(self):
            thread1

timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)

thread = Thread()
thread.newData.connect(update)  #It might be something with row, but removing it doesn't change anything
thread.start()

# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()


vas...@gmail.com

unread,
Jun 1, 2017, 12:03:27 PM6/1/17
to pyqt...@googlegroups.com
I already sent this code once, so this is my way to use threads.

# 'calculate' function is an inside-class thread and main thread use calculated self.y array
import numpy as np
import pyqtgraph as pg
import threading

class FuncThread(threading.Thread):
    def __init__(self,t,*a):
        self._t=t
        self._a=a
        threading.Thread.__init__(
self)
    def run(self):
        self._t(*self._a)

class MyApp():
    def __init__(self):
        self.number_of_data_steps = 100000
        self.qapp = pg.mkQApp()
        self.win = pg.GraphicsWindow()
        plot1 = self.win.addPlot()
        curve = plot1.plot()
        curve.setPen('r')       
        x = np.linspace(0, self.number_of_data_steps, self.number_of_data_steps)
        self.y = np.linspace(-5, 5, self.number_of_data_steps)
       
        calculating_thread = FuncThread(self.calculate)
        calculating_thread.start()           

        while self.win.isVisible():  
            curve.setData(x,self.y)
            self.qapp.processEvents()

    def calculate(self):
        while self.win.isVisible():  
            self.y += np.random.random(self.number_
of_data_steps) - 0.5
                   
if __name__=='__main__': 
    m = MyApp()


--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/f9941f7c-0449-42e9-98c9-bea183da47d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Quinty van Dijk

unread,
Jun 1, 2017, 2:39:39 PM6/1/17
to pyqtgraph
Thanks! The thing I didn't know was when you import an file in python the interpreter executes the code in that file. So I added the if __name =='__main__' to the beginning of the file.
 

Op donderdag 1 juni 2017 18:03:27 UTC+2 schreef Vasilije Mehandzic:
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.

Sebastian Höfer

unread,
Jun 7, 2017, 6:16:39 AM6/7/17
to pyqtgraph
Hi Quinty,

the thread, as you are using it, will not do anything. In your run() method you're using "thread1" as a statement, which is valid, but doesn't do anything. If you want to call code from your module you have to pack it into a function an then call "thread1.your_function_name()". (Have a look at:  https://docs.python.org/2/tutorial/modules.html )

And if you want to use the signal "newData" just to call your "update()" function, you have define it with the same types of parameters (in your case without any): newData = pg.QtCore.Signal()
To trigger the signal call "self.newData.emit()" in the run() function of your thread class.   (I suggest this tutorial:  http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/  )

Cheers,
Sebastian
Reply all
Reply to author
Forward
0 new messages