python - 尝试优化 pyqtgraph 绘图

标签 python pyqt pyqtgraph

我试图在 pyqtgraph 中不使用 pg.PolyLineROI() 来绘制多边形。我的目标是能够使用大型数据集代替下面所示代码中的“数据”。我对 PolyLineROI() 的问题是,我不需要句柄或事件,因此加载大量数据需要很长时间,并且资源浪费在不需要的功能上。

我尝试过使用 QPainter 和 QPen,但我无法得到任何有效的东西,所以我陷入了困境。有什么想法吗?

编辑代码尝试合并来自 segFaultCoder 的示例

from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import sys

class plotwindow(QtGui.QMainWindow):
        def setupUi(self, MainWindow):

            self.centralwidget = QtGui.QWidget(MainWindow)
            MainWindow.resize(1900, 1000)

            self.qt = pg.GraphicsView(MainWindow)
            self.qt.setGeometry(QtCore.QRect(0,0, 1900, 1000))
            self.qt2 = pg.GraphicsLayout()
            self.qt.setCentralItem(self.qt2)
            self.qt.show()
            self.layout = self.qt2.addLayout()
            self.qt3 = self.layout.addViewBox()


            self.plot()

        def plot(self): #This is looped for multiple data sets
            data = [[6,6],[6,0],[0,6],[0,0]] #changes based on data import
            self.picture = QtGui.QPicture()
            p = QtGui.QPainter(self.picture)
            p.setPen(pg.mkPen('w'))
            self.points = []
            for item in data:
                point = QtCore.QPoint(item[0], item[1])
                self.points.append(point)
            p.drawPolygon(*self.points)
            p.end()
            self.qt3.addItem(p)

    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = plotwindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_()) 

最佳答案

我认为您使用 QPainter 的方向是正确的,只需查看他们的示例文件夹,您就会看到一个 customGraphicsItem.py。我基本上只是复制了它并用您的点替换了它们的“数据”变量,然后使用 QPainter.drawPolygon() 重写了generatePicture() 方法。希望我所有的缩进都是正确的,我不太掌握格式化帖子的窍门。

import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui

# Create a subclass of GraphicsObject.
# The only required methods are paint() and boundingRect() 
# (see QGraphicsItem documentation)
class customPolyItem(pg.GraphicsObject):
    def __init__(self, data):
        pg.GraphicsObject.__init__(self)
        self.data = data  
        self.points = []
        self.generatePicture()


    def generatePicture(self):
        # pre-computing a QPicture object allows paint() to run much more quickly, 
        # rather than re-drawing the shapes every time.
        self.picture = QtGui.QPicture()
        p = QtGui.QPainter(self.picture)
        p.setPen(pg.mkPen('w'))
        for item in self.data:
            point = QtCore.QPoint(item[0],item[1])
            self.points.append(point)
        p.drawPolygon(*self.points)
        p.end()

    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        # boundingRect _must_ indicate the entire area that will be drawn on
        # or else we will get artifacts and possibly crashing.
        # (in this case, QPicture does all the work of computing the bouning rect for us)
        return QtCore.QRectF(self.picture.boundingRect())

data = [[6,6],[6,0],[0,6],[0,0]]

item = customPolyItem(data)
plt = pg.plot()
plt.addItem(item)
plt.setWindowTitle('pyqtgraph example: customPolyItem')

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

编辑:关于您的新代码,用此替换您的绘图方法(并删除油漆和边界矩形):

    def plot(self): #This is looped for multiple data sets
        data = [[6,6],[6,0],[0,6],[0,0]] #changes based on data import
        newCPI = customPolyItem(data) # create new instance with changed data
        self.qt3.addItem(newCPI) # add the new instance to your viewbox

如果 customPolyItem 类位于单独的文件中,则需要导入它,或者只需将整个类声明复制粘贴到当前的plotwindow 类下方。

关于python - 尝试优化 pyqtgraph 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40312843/

相关文章:

python - 绘制实时传感器数据时,PyQtGraph 停止更新并卡住

python - 寻找三角形镶嵌的最近邻

python - PEP8 E128 : can't figure out why a line is being flagged

python - pyqt导入问题

python - 装饰器添加了一个意想不到的参数

python - 尝试用 pyqtgraph 做一个简单的撤消方法

python - 在网络上显示 pyqtgraph 和 pyqt 小部件

python - Django_tables2 - 部分 css 不工作

python - 列表中的列表

python - 获取选定的行 pyqt5 qtablewidget