我必须绘制从传感器读取的数据的 3 条更新曲线。当我只使用一条曲线时,更新图非常快,但是当我尝试绘制它们时,它们中的每一个都非常慢。我使用的代码如下:
#!/usr/bin/python
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import time
import numpy as np
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
p1 = win.addPlot()
p2 = win.addPlot()
p3 = win.addPlot()
curve1 = p1.plot()
curve2 = p2.plot()
curve3 = p3.plot()
readData = [0.0, 0.0, 0.0]
y1=[0.0]
y2=[0.0]
y3=[0.0]
temp = [0.0]
start = time.time()
def update():
global curve1, curve2, curve3
t = time.time()-start # measure of time as x-coordinate
readData= readfun() #function that reads data from the sensor it returns a list of 3 elements as the y-coordinates for the updating plots
y1.append(readData[0])
y2.append(readData[1])
y3.append(readData[2])
temp.append(t)
curve1.setData(temp,y1)
curve2.setData(temp,y2)
curve3.setData(temp,y3)
app.processEvents()
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_'):
QtGui.QApplication.instance().exec_()
如何加快三条曲线的更新绘图?谢谢
编辑:受 dirkjot 解决方案的启发,我想编辑上面的代码,以防有人出于相同目的需要它。它工作正常:
#!/usr/bin/python
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import time
import numpy as np
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
p1 = win.addPlot()
p2 = win.addPlot()
p3 = win.addPlot()
curve1 = p1.plot()
curve2 = p2.plot()
curve3 = p3.plot()
readData = [0.0, 0.0, 0.0]
y1=np.zeros(1000,dtype=float)
y2=np.zeros(1000,dtype=float)
y3=np.zeros(1000,dtype=float)
indx = 0
def update():
global curve1, curve2, curve3, indx, y1,y2,y3
readData= readfun() #function that reads data from the sensor it returns a list of 3 elements as the y-coordinates for the updating plots
y1[indx]=readData[0]
y2[indx]=readData[1]
y3[indx]=readData[2]
if indx==99:
y1=np.zeros(1000,dtype=float)
y2=np.zeros(1000,dtype=float)
y3=np.zeros(1000,dtype=float)
else:
indx+=1
curve1.setData(y1)
curve2.setData(y2)
curve3.setData(y3)
app.processEvents()
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_'):
QtGui.QApplication.instance().exec_()