定长数据窗口的滚动显示
这种折线图只会显示最近n个数据的窗口,想拖动plotwidget查看不了从0开始的历史数据,代码实现如下
...
self.data = [] # 绘图数据
self.win_len = 20 # 窗口长度
self.ploterPos= 0 # 绘图位置
self.ploter = self.plotWidget.plot # 绘图handler
def on_button_clicked(self):
new_data = 50 + 10*np.random.normal()
# 数据大于窗口长度
if len(self.data) > self.win_len:
self.step += 1
# 更新窗口长度区间
self.plotWidget.setXRange(0+self.ploterPos, self.win_len+self.ploterPos)
# 后移绘图起点位置
self.ploter.setPos(self.ploterPos, 0)
# 滚动数据
self.data[:-1] = self.data[1:]
self.data[-1] = new_data
# 数据小于窗口长度
else:
# 保持基础窗口长度
self.plotWidget.setXRange(0, self.win_len)
# 追加数据
self.data.append(new_data )
# 使用setData刷新数据绘图
self.ploter.setData(self.data, pen='g')
...
保留历史数据的滚动显示
...
self.data = [] # 绘图数据
self.win_len = 20 # 窗口长度
self.ploterPos= 0 # 绘图位置
self.ploter = self.plotWidget.plot # 绘图handler
def on_button_clicked(self):
new_data = 50 + 10*np.random.normal()
# 数据大于窗口长度
if len(self.data) > self.win_len:
self.step += 1
# 更新窗口长度区间
self.plotWidget.setXRange(0+self.ploterPos, self.win_len+self.ploterPos)
# 数据小于窗口长度
else:
# 保持基础窗口长度
self.plotWidget.setXRange(0, self.win_len)
# 追加数据
self.data.append(new_data )
# 使用setData刷新数据绘图
self.ploter.setData(self.data, pen='g')
...