我想从一个列表选择中在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))


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

最佳答案

我在这里找到解决方案:
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))

关于python-2.7 - pyqtgraph删除pyqt4 gui中的持久性图例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42792858/

10-12 19:28
查看更多