我正在尝试使用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'])
答案 0 :(得分:3)
您的代码看起来很好。根据您的系统,pyqtgraph的散点图效率会降低10k到100k左右。如果你真的想继续使用pyqtgraph,我可以推荐的是将你的数据子采样10倍到100倍。
您想要显示的数据量几乎需要GPU加速,因此Galry是一个很好的工具。仅供参考,pyqtgraph,Galry和其他一些python图形库的开发人员正在开发VisPy,它还没有完全可以使用,但在未来应该是一个非常好的选择。 PyQtGraph也将在未来使用VisPy提供GPU加速。