尝试优化 pyqtgraph 绘图
Trying to optimize pyqtgraph plotting
我想在 pyqtgraph 中不使用 pg.PolyLineROI() 来绘制多边形。我的目标是能够在下面显示的代码中使用大数据集代替 "data"。我对 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。我基本上只是复制了它并用你的观点替换了他们的 "data" 变量,然后使用 QPainter.drawPolygon() 重写了 generatePicture() 方法。希望我所有的缩进都是正确的,我不太了解 SO 上的格式化帖子。
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_()
编辑:关于你的新代码,用这个替换你的 plot 方法(并删除 paint 和 boundingrect):
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 class 在单独的文件中,则需要导入它,或者只需将整个 class 声明复制粘贴到当前绘图窗口下方 class。
我想在 pyqtgraph 中不使用 pg.PolyLineROI() 来绘制多边形。我的目标是能够在下面显示的代码中使用大数据集代替 "data"。我对 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。我基本上只是复制了它并用你的观点替换了他们的 "data" 变量,然后使用 QPainter.drawPolygon() 重写了 generatePicture() 方法。希望我所有的缩进都是正确的,我不太了解 SO 上的格式化帖子。
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_()
编辑:关于你的新代码,用这个替换你的 plot 方法(并删除 paint 和 boundingrect):
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 class 在单独的文件中,则需要导入它,或者只需将整个 class 声明复制粘贴到当前绘图窗口下方 class。