添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

移除/删除PyQtGraph中的图例

2 人关注

在实时图表上创建了两条线并添加了一个图例。在下次更新时,使用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_())
    
python
pyqt5
data-visualization
pyqtgraph
paradoxarium
paradoxarium
发布于 2020-03-22
1 个回答
eyllanesc
eyllanesc
发布于 2020-03-22
已采纳
0 人赞同

你有一个 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__":