【问题标题】:Remove / Delete Legend in PyQtGraph删除/删除 PyQtGraph 中的图例
【发布时间】:2020-07-03 00:41:51
【问题描述】:

在实时图表上创建两条线并添加一个图例。下次更新时,图表上的线条会使用 self.pw.clear() 删除。

但是图例并没有被删除,而且每次更新都会添加一个新的图例实例,数量很多,而且计划更新的FPS迅速下降。

这里 http://www.pyqtgraph.org/documentation/graphicsItems/plotitem.html 说 Clear():“从 ViewBox 中删除所有项目。”

尝试清除/删除项目 - 尚未帮助(语法不正确,或过程调用不正确)。

如何在更新图表时删除图例或停止创建多个图例?

import random
from PyQt5 import QtGui
import pyqtgraph as pg
import sys

class Mainwindow(QtGui.QMainWindow):
    def __init__(self, parent):
        super(Mainwindow, self).__init__()
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.resize(1000, 500)

        self.vbox = QtGui.QVBoxLayout()
        self.pw = pg.PlotWidget()
        self.vbox.addWidget(self.pw)
        self.centralWidget.setLayout(self.vbox)

        # Update chart
        self.timer = pg.QtCore.QTimer()
        self.timer.setSingleShot(False)
        self.timer.timeout.connect(self.update)
        self.timer.start(10)

    def update(self):
        x = []
        y = []
        z = []
        for i in range(10000):
            x.append(i)
            y.append(random.uniform(0, 1))
            z.append(1 + random.uniform(0, 1))

        self.pw.clear()
        line_red = pg.PlotCurveItem(x, y, clear=True, pen='r', name='Red')
        line_yellow = pg.PlotCurveItem(x, z, clear=True, pen='y', name='Yellow')
        self.pw.addItem(line_red)
        self.pw.addItem(line_yellow)
        self.pw.addLegend()

app = QtGui.QApplication(sys.argv)
ex = Mainwindow(app)
ex.show()
sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt5 data-visualization pyqtgraph


    【解决方案1】:

    你有一个XY problem,而不是问如何更新情节?问题 如何消除重复的图例?所以我会指出根本问题的解决方案。

    考虑到上述情况,逻辑是只创建一次项目并使用setData()方法更新信息。

    import random
    import sys
    
    import pyqtgraph as pg
    from pyqtgraph.Qt import QtGui
    
    
    class Mainwindow(QtGui.QMainWindow):
        def __init__(self, parent):
            super(Mainwindow, self).__init__()
            self.centralWidget = QtGui.QWidget()
            self.setCentralWidget(self.centralWidget)
            self.resize(1000, 500)
    
            vbox = QtGui.QVBoxLayout(self.centralWidget)
            self.pw = pg.PlotWidget()
            self.pw.addLegend()
            vbox.addWidget(self.pw)
    
            # Update chart
            self.timer = pg.QtCore.QTimer()
            self.timer.setSingleShot(False)
            self.timer.timeout.connect(self.update)
            self.timer.start(10)
    
            self.line_red = pg.PlotCurveItem(clear=True, pen="r", name="Red")
            self.line_yellow = pg.PlotCurveItem(clear=True, pen="y", name="Yellow")
    
            self.pw.addItem(self.line_red)
            self.pw.addItem(self.line_yellow)
    
        def update(self):
            x = []
            y = []
            z = []
            for i in range(10000):
                x.append(i)
                y.append(random.uniform(0, 1))
                z.append(1 + random.uniform(0, 1))
            self.line_red.setData(x, y)
            self.line_yellow.setData(x, z)
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        ex = Mainwindow(app)
        ex.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 是的,现在我明白了我的错误。您的代码工作正常。此外,由于您对 setData() 的决定,我还比较了您的代码和我的代码的性能(都没有图例)-您的代码增加了 FPS 的 +14..19%。酷!
    猜你喜欢
    • 2017-08-05
    • 2016-12-24
    • 2020-05-02
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    相关资源
    最近更新 更多