使用PyQtGraph绘制数据滚动图形(3)
import pyqtgraph as pg import numpy as np from pyqtgraph.Qt import QtGui, QtCore app = pg.QtGui.QApplication([]) win = pg.GraphicsWindow(title="数据滚动") win.resize(600,300) p = win.addPlot() data = np.random.random(size=50) curve = p.plot(data) def update(): global data, curve data[:-1] = data[1:] #数据向左滚动1位,也可以用data = np.roll(data,-1)实现 data[-1] = np.random.random() #末位更改为随机值 curve.setData(data) timer = QtCore.QTimer() timer.timeout.connect(update) timer.start(100) app.exec_()
上面的图形,数据更新后,横坐标并没有跟随新的数据做出改变,这里可以用setPos来实现
import pyqtgraph as pg import numpy as np from pyqtgraph.Qt import QtGui, QtCore app = pg.QtGui.QApplication([]) win = pg.GraphicsWindow(title="数据滚动") win.resize(600,300) p = win.addPlot() data = np.random.random(size=50) curve = p.plot(data) ptr=0 def update(): global data, curve, ptr data[:-1] = data[1:] #数据向左滚动1位,也可以用data = np.roll(data,-1)实现 data[-1] = np.random.random() #末位更改为随机值 curve.setData(data) ptr += 1 curve.setPos(ptr,0) timer = QtCore.QTimer() timer.timeout.connect(update) timer.start(100) app.exec_()