如何避免在PyQtGraph的FillBetweenItem中第二次调用updatePath以便加快绘图速度?

问题描述

我有一个应用程序,需要在其中使用FillBetweenItem的多个实例。分析时,我注意到更新路径花费的时间最长。首先(紧随其后的是mwe),是否仍然可以加快updatepath函数的调用?在项目之间仅填充一个并设置区域而不在同一行的上下行填充会有所帮助吗?

在分解的过程中,我注意到,对于FillBetweenItem的初始化,updatePath方法被调用了两次。我可以避免打一个电话,还是有其他方法可以加快通话速度?

这是我最小的工作示例:

        """
        mweFill area
        """
        import pyqtgraph as pg
        import cProfile
        import pstats
        import numpy as np
        ZHEIGHT = 100
        class FillAreaItem:
            def __init__(self,arrX,arrY,arrBaseline,ax,color = '#660666'):
                self.arrX = arrX
                self.arrY = arrY
                self.arrBaseline = arrBaseline
                self.ax = ax
                self.color = color
                self.linesFill = {'upperLines': None,'lowerLines': None,'fill': None }
                self.fillArea()
            def fillArea(self,ax = None):
                """
    fill area
                """
                if ax is not None:
                    self.ax = ax
                else:
                    if self.ax is None:
                        return []
                
                brushFill = pg.mkBrush(color = self.color)
                pen = pg.mkPen(width = 0)
                if self.linesFill['fill'] is not None and self.linesFill['fill'] in self.ax.items():
                    self.linesFill['fill'].curves[0].sigPlotChanged.disconnect()
                    self.linesFill['fill'].curves[1].sigPlotChanged.disconnect()
                if self.linesFill['upperLines'] is None  or not(self.linesFill['upperLines'] in self.ax.items()):
                    self.linesFill['upperLines'] =  self.ax.plot(self.arrX,self.arrY,pen = pen,ignoreBounds = True)
                else:
                    self.linesFill['upperLines'].setData(x = self.arrX,y =self.arrY)
                    self.linesFill['upperLines'].setPen(pen)
                if self.linesFill['lowerLines'] is None  or not(self.linesFill['lowerLines'] in self.ax.items()):
                    self.linesFill['lowerLines'] =  self.ax.plot(self.arrX,self.arrBaseline,pen             = pen,ignoreBounds = True)
                else:
                    self.linesFill['lowerLines'].setData(x = self.arrX,y =self.arrBaseline)
                    self.linesFill['lowerLines'].setPen(pen)
                if self.linesFill['fill'] is None  or not(self.linesFill['fill'] in             self.ax.items()):
                    self.linesFill['fill'] =  pg.FillBetweenItem( self.linesFill['lowerLines'],self.linesFill['upperLines'],brush = brushFill)
                    self.ax.addItem(self.linesFill['fill'],ignoreBounds = True)
    
                if self.linesFill['fill'] is not None and self.linesFill['fill'] in self.ax.items():
                                self.linesFill['fill'].curves[0].sigPlotChanged.connect(self.linesFill['fill'].curveChanged)
                    self.linesFill['fill'].curves[1].sigPlotChanged.connect(self.linesFill['fill'].curveChanged)
                self.linesFill['fill'].setZValue(ZHEIGHT)
                self.linesFill['fill'].setBrush(brushFill)
        if __name__ == '__main__':
            import sys
            #create some data
            arrX = np.arange(10000)
            arrY = 2+np.random.rand(10000)
            arrBaseline = 0.5+np.random.rand(10000)
            #application to run window
            app = pg.Qt.QtGui.QApplication(sys.argv)
            ax = pg.PlotWidget()
            ax.show()
            #create instance to plot
            FillItem = FillAreaItem(arrX,ax)
            #profile draw function
            pr = cProfile.Profile()
            pr.enable()        
            # FillItem = FillItem.fillArea()
            FillItem = FillAreaItem(arrX,ax)
            pr.disable()
            pr.print_stats(sort='cumulative')

            if (sys.flags.interactive != 1) or not hasattr(pg.Qt.QtCore,'PYQT_VERSION'):
                pg.Qt.QtGui.QApplication.instance().exec_()

这是在初始化过程中分析的第一行:

 3682 function calls (3644 primitive calls) in 0.083 seconds

排序依据:累积时间

    ncalls  tottime  percall  cumtime  percall filename:lineno(function)

    1    0.000    0.000    0.083    0.083 mweFillArea.py:10(__init__)
    1    0.000    0.000    0.083    0.083 mweFillArea.py:18(fillArea)
    1    0.000    0.000    0.073    0.073 FillBetweenItem.py:10(__init__)
    2    0.063    0.032    0.073    0.036 FillBetweenItem.py:60(updatePath)
    1    0.000    0.000    0.047    0.047 FillBetweenItem.py:29(setCurves)
    1    0.000    0.000    0.047    0.047 FillBetweenItem.py:57(curveChanged)
    2    0.000    0.000    0.008    0.004 PlotItem.py:589(plot)
    3    0.000    0.000    0.006    0.002 PlotItem.py:475(addItem)
    3    0.000    0.000    0.006    0.002 ViewBox.py:394(addItem)
    2    0.005    0.002    0.005    0.002 {method 'addPolygon' of 'PySide2.QtGui.QPainterPath' objects}
    7    0.000    0.000    0.004    0.001 {function GraphicsItem.setParentItem at 0x0000023CC8ADB3A8}
    6    0.000    0.000    0.003    0.001 ViewBox.py:826(updateAutoRange)
    6    0.000    0.000    0.003    0.001 GraphicsItem.py:381(setParentItem)
    4    0.000    0.000    0.003    0.001 PlotCurveItem.py:440(getPath)
    2    0.000    0.000    0.003    0.002 PlotCurveItem.py:416(generatePath)
    2    0.003    0.001    0.003    0.002 functions.py:1470(arrayToQPath)
   51    0.000    0.000    0.003    0.000 GraphicsObject.py:20(itemChange)
    2    0.000    0.000    0.003    0.001 PlotDataItem.py:37(__init__)
   12    0.000    0.000    0.002    0.000 GraphicsItem.py:437(parentChanged)
16/12    0.000    0.000    0.002    0.000 GraphicsItem.py:445(_updateView)

任何建议将不胜感激,非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...