在实时图表上创建了两条线并添加了一个图例。在下次更新时,使用self.pw.clear()删除图表上的线条。
但是图例并没有被删除,每次更新都会增加一个新的图例,数量很多,日程表更新的FPS很快就下降了。
Here 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_())