我必须绘制从传感器读取的数据的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_()
答案 0 :(得分:4)
一个问题是您要附加到列表中。当列表变大时,这会变得非常慢,因为Python必须将完整列表复制到比前一个大一个单元格的新位置。您可以通过在删除附加的情况下再次运行代码来测试这一点,但继续读取数据(您为该测试丢弃的数据)。
如果这是问题,有几种解决方案:
[None] * 1000
)并在你写作的地方保留一个计数器。达到1k时缠绕。通过这种方式,您可以获得持续更新的显示效果,就像您在老式示波器上看到的一样。buffer[0:-10] = buffer[10:]
,但可能有一个专用的numpy shift指令)中将缓冲区内容10移到左边,然后再次开始写入-10。