pyqtgraph删除pyqt4 gui

时间:2017-03-14 17:35:31

标签: python-2.7 plot pyqt pyqt4 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))

如何销毁每次运行的旧图例?

1 个答案:

答案 0 :(得分:4)

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

我需要添加它(不确定是否需要try / except):

    try:
        self.legend.scene().removeItem(self.legend)
    except Exception as e:
        print e

最终代码看起来像这样:

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