Search code examples
pythonpyqt5data-visualizationpyqtgraph

Remove / Delete Legend in PyQtGraph


Two lines are created on the live chart and a legend is added. At the next update, the lines on the chart are deleted using self.pw.clear().

But the legend is not deleted, and with each update, a new instance of the legend is added, there are a lot of them and the FPS of the schedule update quickly drops.

Here http://www.pyqtgraph.org/documentation/graphicsItems/plotitem.html says Clear(): “Remove all items from the ViewBox.”

Attempting to Clear / removeItem - have not yet helped (either the syntax is incorrect, or the procedure call is incorrect).

How to delete a legend when updating a chart or stop multiple legends creation?

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_())

Solution

  • You have an XY problem, instead of asking How to update the plot? questions How to eliminate the duplicate legend ?. So I will point out a solution to the underlying problem.

    Considering the above, the logic is to only create the items once and update the information using the setData() method.

    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_())