pyqtgraph 删除 pyqt4 gui 中的持久图例

las*_*hon 2 plot pyqt pyqt4 python-2.7 pyqtgraph

我想从列表选择中绘制 pyQt4 gui 中的多个项目,用户可以选择要显示的图。他们可以根据需要多次执行此操作。每次他们绘制新数据时,即使绘图没有,图例仍然存在。我的代码是:

self.DataPlotter.setLabels(left=("magnitude"),bottom=(str(x_ind)))
title = str(y_ind) + " vs " + str(x_ind)
self.DataPlotter.setTitle(title)
self.DataPlotter.addLegend()

for y,c in zip(y_ind,range(len(y_ind))):
    self.DataPlotter.plot(self.df[x_ind].tolist(),self.df[y].tolist(), name=y, pen=(c,4))
Run Code Online (Sandbox Code Playgroud)

我如何在每次运行时摧毁旧传说?

las*_*hon 5

我在这里找到了解决方案:https : //groups.google.com/forum/#!topic/pyqtgraph/DdWyB1ljQdw

我需要添加这个(不确定是否需要尝试/除外):

    try:
        self.legend.scene().removeItem(self.legend)
    except Exception as e:
        print e
Run Code Online (Sandbox Code Playgroud)

最终代码如下:

        self.DataPlotter.setLabels(left=("magnitude"),bottom=(str(self.x_ind)))
        title = str(self.y_ind) + " vs " + str(self.x_ind)
        self.DataPlotter.setTitle(title)
        try:
            self.legend.scene().removeItem(self.legend)
        except Exception as e:
            print e
        self.legend = self.DataPlotter.addLegend()
        for y,c in zip(y_ind,range(len(y_ind))):
           self.DataPlotter.plot(self.df[x_ind].tolist(),self.df[y].tolist(), name=y, pen=(c,4))
Run Code Online (Sandbox Code Playgroud)