pyqtgraph清空画布

有一个场景,实时检测数据并做实时绘图,应为实时绘图时,数据量太大会导致pyqtgraph界面无响应,这时候要每隔一段时间本地化保存一下数据,并清空当前画布,应为画布中有十字交叉线,清空画布时,不想将其也清除掉,另外还有一点就是,清空画布属于耗时操作,最好放到线程中执行,不然每次清空时,都会出现ANR,一段时间后会恢复

pyqtgraph源代码中有两个清除操作,分别如下:

clear()方法会清除plot上的所有item,包括图例,以及自定义的十字交叉线等等

而clearPlots()方法只会清除当前的绘画点,其他的item会保留,根据自己的需求适当应用

def clear(self):
    """
    Remove all items from the PlotItem's :class:`~pyqtgraph.ViewBox`.
    """
    for i in self.items[:]:
        self.removeItem(i)
    self.avgCurves = {}
def clearPlots(self):
    for i in self.curves[:]:
        self.removeItem(i)
    self.avgCurves = {}     

代码参考:

self.win = pg.GraphicsLayoutWidget(show=True)                       
self.label = pg.LabelItem(justify='right')                          

self.win.addItem(self.label) 

self.plot_plt = self.win.addPlot(row=1, col=0)

# 清空操作
# self.plot_plt.clear()                         # 清空绘画-->会清空所有,比如图例,十字交叉线
# self.plot_plt.clearPlots()                    # 清空绘画-->只会清空当前的绘图                                       

你可能感兴趣的:(PyQt5,pyqtgraph,PyQt5)