在pyqt5中使用PlotWidget.plot()后如何清除数据?

问题描述 投票:1回答:1

我的代码是:

class Ui_Registos2(object):
    def setupUi(self, Registos2, Info):

        self.Grafico_2 =  pg.PlotWidget(self.centralwidget)
        self.Grafico_2.setGeometry(QtCore.QRect(540, 430, 501, 331))
        self.Grafico_2.setObjectName("Grafico_2")       

    def openfile(self):
        name = QFileDialog.getOpenFileName()
        filename = name[0]

       if len(filename) <=0:
           return
       try:            
           self.Info.file = open(filename, 'r')

       except NameError:          
           return

       self.Info.df = pd.read_excel(filename, sheet_name='Tabela', encoding="utf-8")

       i = 0
       items = []

        # TABLE HEADER
       for field in self.Info.df.columns:
           items.append(QtGui.QStandardItem(field))

       self.model.appendRow(items)        
       n = [] # CADA LINHA DA TABELA        
       while i<len(self.Info.df):

           linha2 = self.Info.df.loc[i] 
           n = []
           for item in linha2:
               print(item)
               n.append(QtGui.QStandardItem(str(item))) 

           self.model.appendRow(n) 
           i+=1 
           #FIRST GRAPH              
           self.y =self.Info.df.loc[:,'%']
           self.x =self.Info.df.loc[:,'t']

           pen = pg.mkPen(color=(255, 0, 0))
           self.data_line =  self.Grafico.plot(self.x, self.y, pen=pen)     
           self.plot = self.pw.plot(x, y, pen=None, symbol="o", symbolBrush="r")       

因此,每当我单击按钮'openfile'时,我都希望清除现有数据并重新绘制新数据,但是PlotWidget会在现有图形上重写。有什么方法可以清除数据?谢谢。

python pyqt pyqt5 pyqtgraph
1个回答
0
投票

您可以使用plot.clear()清除图。我无法测试您的示例,但这应该可以工作:

self.Grafico.clear()
self.pw.clear()
© www.soinside.com 2019 - 2024. All rights reserved.