使用PyQtGraph有效绘制大型数据集

3
我试图使用pyqtgraph生成散点图和直方图的矩阵。每个散点图的输入(x和y值)都是长度大于1,000,000的numpy数组。生成这些图需要很长时间(2x2矩阵需要超过1分钟),而matplotlib在生成相同类型的图时速度更快。有没有什么方法可以加快速度?下面是我使用的代码。
谢谢。
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

def scatter_matrix(data, cols):
    pg.setConfigOption('background', 'w')
    pg.setConfigOption('foreground', 'k')
    now = pg.ptime.time()

    app = QtGui.QApplication([])

    win = pg.GraphicsWindow(title="Scater Plot Matrix")
    win.resize(800,600)

    for i, col_i in enumerate(cols):
        for j, col_j in enumerate(cols):
            x = data[col_i]
            y = data[col_j]
            if i == j:
                current_plot = win.addPlot(title="Histogram")
                y,x = np.histogram(x, bins=100)
                curve = pg.PlotCurveItem(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 80))
                current_plot.addItem(curve)
            else:
                current_plot = win.addPlot(title="Scatter plot")
                current_plot.plot(x, y, pen=None, symbol='t', symbolPen=None, symbolSize=10, symbolBrush=(100, 100, 255, 50))
                current_plot.setLabel('left', "{}".format(col_i), units='')
                current_plot.setLabel('bottom', "{}".format(col_j), units='')
                current_plot.setLogMode(x=False, y=False)
        win.nextRow()
    ## Start Qt event loop unless running in interactive mode or using pyside.
    import sys
    print "Plot time: %0.2f sec" % (pg.ptime.time()-now)
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        app.exec_()

data = {}
for key in ['a','b']:
    data[key] = np.random.normal(size=(1000000), scale=1e-5)

scatter_matrix(data,['a','b'])

在网上搜索了很多后,我最终尝试了基于GPU的绘图库Galry。结果加速超过了100倍!以下是代码。虽然如此,我仍然想知道是否有使用PyQtGraph加速绘图的方法。

import numpy as np
from galry import *
import time

class MyPaintManager(PlotPaintManager):
    def initialize(self):
        if self.parent.visual == BarVisual:
            self.add_visual(self.parent.visual, self.parent.x, primitive_type= self.parent.plot_type, color='b')
        elif self.parent.visual == PlotVisual:
            self.add_visual(self.parent.visual, x=self.parent.x, y=self.parent.y, primitive_type= self.parent.plot_type, color='b')

class MyWidget(GalryWidget):
    def initialize(self, x, y, visual, title=None, plot_type=None):
        self.activate_grid = True
        self.show_grid = True

        self.x = x
        self.y = y
        self.visual = visual
        self.plot_type = plot_type
        self.title = title

        self.set_bindings(PlotBindings)
        self.set_companion_classes(
            paint_manager=MyPaintManager,
            interaction_manager=PlotInteractionManager,)
        self.initialize_companion_classes()

def scatter_matrix(df, cols):
    now = time.time()

    class Window(QtGui.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.initUI()

        def initUI(self):
            vbox = QtGui.QGridLayout()
            self.setLayout(vbox)
            self.setGeometry(300, 300, 600, 600)
            for i, col_i in enumerate(cols):
                for j, col_j in enumerate(cols):
                    x = df[col_i]
                    y = df[col_j]
                    if i == j:
                        y,x = np.histogram(x, bins=100)
                        vbox.addWidget(MyWidget(x=y,y=y, visual = BarVisual, title='{}_vs_{}'.format(col_i, col_j)), i, j)
                    else:
                        vbox.addWidget(MyWidget(x=x,y=y, visual = PlotVisual, title='{}_vs_{}'.format(col_i, col_j), plot_type='POINTS'), i, j)

            print "Plot time: %0.2f sec" % (time.time()-now)
            self.show()

    show_window(Window)

if __name__ == '__main__':
    data = {}
    for key in ['a','b']:
        data[key] = np.random.normal(size=(1000000), scale=1e-5)

    scatter_matrix(data,['a','b'])
1个回答

3

你的代码看起来很好。根据你的系统,当使用pyqtgraph绘制10k到100k个点时,散点图的效率会下降。如果你真的想继续使用pyqtgraph,我唯一能建议的是对数据进行10倍到100倍的子采样。

你想要可视化的数据量几乎需要GPU加速,因此Galry是在这里使用的好工具。值得一提的是,pyqtgraph、Galry和其他一些python图形库的开发人员正在共同开发VisPy,虽然它目前还不能使用,但未来应该是一个非常好的选择。PyQtGraph也将使用VisPy来实现GPU加速。


感谢提供这些信息。我确实了解到VisPy,意识到它还处于早期开发阶段。一旦该项目成熟,我会尝试使用它。 - user3520133
@Luke PyQtGraph现在是否具有GPU加速功能? - eric
不同于我之前预测的方式,但是最近有一些戏剧性的性能改进(我相信这些使用了GPU和numba)来加速其图形渲染管道。我建议从GitHub测试最新版本以查看它是否符合您的需求。 - Luke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接