You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQtGraph滚动模式下添加ROI的问题及数据留存需求

PyQtGraph实时滚动折线图ROI与历史数据问题解决方案

一、让ROI随折线图滚动移出视图

你当前把ROI添加到了LivePlotWidget(视图容器),而非实际的绘图场景(PlotItem),导致ROI固定在视图层,不会随数据坐标系滚动。解决方法是将ROI添加到绘图场景中:

修改addRect函数,把ROI添加到time_axis_plot_widget的PlotItem上:

def addRect(button):
    print('Added')
    roi= pg.ROI((time.time()-3,-1.5),(2,3),pen='g',movable=False)
    roi.setZValue(10)
    # 改为添加到PlotItem,而非LivePlotWidget
    time_axis_plot_widget.getPlotItem().addItem(roi)

这样ROI会处于数据坐标系中,当图表滚动时,会和数据一起移出或进入视图。

二、保留历史数据及已添加的ROI

当前DataConnector设置了max_points=600,超过该数量后会自动删除旧数据,导致历史数据被覆盖。要保留历史数据,只需调整max_points为更大的数值(根据你的内存需求设置),同时ROI基于时间坐标创建,会始终保留在对应的历史位置:

修改DataConnector的初始化代码:

# 增大max_points值,比如设置为10000,按需调整
connectors.append(DataConnector(plot, max_points=10000))

如果需要无限保留历史数据,可以不设置max_points(但需注意内存占用,大量数据会导致性能下降)。

修改后的完整代码

import signal
import time
from math import sin
from threading import Thread
from time import sleep
import pyqtgraph as pg
from pglive.kwargs import Axis
from pglive.sources.data_connector import DataConnector
from pglive.sources.live_axis import LiveAxis
from pglive.sources.live_plot import LiveLinePlot
from pglive.sources.live_plot_widget import LivePlotWidget
import sys
from PyQt5.QtWidgets import QApplication, QPushButton

running = True
app = QApplication(sys.argv)
button = QPushButton('Add Rect')


def addRect(button):
    print('Added')
    roi= pg.ROI((time.time()-3,-1.5),(2,3),pen='g',movable=False)
    roi.setZValue(10)
    # 将ROI添加到PlotItem中
    time_axis_plot_widget.getPlotItem().addItem(roi)

button.clicked.connect(addRect)

def stop():
    """Stop current QApplication"""
    global running
    running = False
    app.exit(0)

# Connect SIGINT with stop function
signal.signal(signal.SIGINT, lambda sig, frame: stop())


connectors = []
layout = pg.LayoutWidget()
# Define Time plot
left_axis = LiveAxis("left", axisPen="red", textPen="red")
bottom_axis = LiveAxis("bottom", axisPen="green", textPen="green", **{Axis.TICK_FORMAT: Axis.TIME})
time_axis_plot_widget = LivePlotWidget(title="Time Line Plot @ 100Hz",
                                       axisItems={'bottom': bottom_axis, 'left': left_axis})
plot = LiveLinePlot()
time_axis_plot_widget.addItem(plot)
# 增大max_points以保留更多历史数据
connectors.append(DataConnector(plot, max_points=10000))
layout.addWidget(time_axis_plot_widget)
layout.addWidget(button)
layout.show()


def sin_wave_generator(*data_connectors):
    """Sinus wave generator"""
    x = 0
    while running:
        x += 1
        for data_connector in data_connectors:
            data_connector.cb_append_data_point(sin(x * 0.01), time.time())
        sleep(0.01)


Thread(target=sin_wave_generator, args=connectors).start()
signal.signal(signal.SIGINT, lambda sig, frame: stop())
app.exec()
stop()

内容的提问来源于stack exchange,提问作者aven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 20:55:20