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