I'm currently writing a python script with pyserial and pyqtgraph that plots data coming in from an accelerometer via the serial port. I append this data to an int array and use it to update the plot. At the moment, my graph width is 500 (I'm only displaying the most recent 500 elements) and I "roll" or "scroll" by appending an element to the end and popping from the start.
data1 = [0] * 500
def update():
global curve1, data1, x, ser
if(ser != None): ## serial object. defined via a QPushButton event
line = ser.readline()
csv = line.decode().split(',')
if len(csv) == 2:
data1.append(int(csv[1]))
data1.pop(0)
xdata1 = np.array(data1[-500:], dtype='int')
curve1.setData(xdata1)
x += 1
curve1.setPos(x, 0)
app.processEvents()
This update method is called by the QtTimer object to update the plot window
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
This way I avoid an array that keeps growing and also avoid copying values by shifting the elements left. My plot however is inconsistent and occasionally lags (When I move the sensor, the plot reacts seconds later). Is this due to me rolling my data inefficiently?