使用PyQtGraph绘制数据滚动图形(4)
app = pg.QtGui.QApplication([]) win = pg.GraphicsWindow(title="数据滚动") win.resize(600,300) p = win.addPlot() # Use automatic downsampling and clipping to reduce the drawing load p.setDownsampling(mode='peak') p.setClipToView(True) curve = p.plot() data = np.empty(100) ptr = 0 def update(): global data, ptr data[ptr] = np.random.normal() ptr += 1 if ptr >= data.shape[0]: #当ptr大于等于data的长度时,改变data的长度 tmp = data data = np.empty(data.shape[0] * 2) data[:tmp.shape[0]] = tmp curve.setData(data[:ptr]) timer = QtCore.QTimer() timer.timeout.connect(update) timer.start(100) app.exec_()
以上的图形中包含从“0”时刻开始的所有数据,随着数据的不断增加,坐标也随之变化。
app = pg.QtGui.QApplication([]) win = pg.GraphicsWindow(title="数据滚动") win.resize(600,300) p = win.addPlot() # Use automatic downsampling and clipping to reduce the drawing load p.setDownsampling(mode='peak') p.setClipToView(True) p.setRange(xRange=[-100,0])#将横坐标的范围限定为[-100,0] p.setLimits(xMax=0)# 横坐标的最大值为0 curve = p.plot() data = np.empty(100) ptr = 0 def update(): global data, ptr data[ptr] = np.random.normal() ptr += 1 if ptr >= data.shape[0]: tmp = data data = np.empty(data.shape[0] * 2) data[:tmp.shape[0]] = tmp curve.setData(data[:ptr]) curve.setPos(-ptr, 0)# timer = QtCore.QTimer() timer.timeout.connect(update) timer.start(100) app.exec_()