如何使用PyQtGraph和ScatterPlotItem(> 1 fps)快速更新图形?

时间:2016-11-12 18:41:01

标签: python matplotlib pyqtgraph

大家都是PyQtGraph用户,

我正在尝试在pg.ScatterPlotItem中使用Matplotlibs喷射颜色,以使用来自数据记录器的数据进行实时图形更新。我在以下几行中将喷射颜色转换为RGBA代码:

z=np.random.randint(0,10000,10000) #random z value
norm = mpl.colors.Normalize(vmin=min(z), vmax=max(z))
m = cm.ScalarMappable(norm=norm, cmap=cm.jet)
colors =  m.to_rgba(z, bytes=True)

我注意到,如果点数为10k或更高,散点图更新中的速度可能会成为问题。例如,在我的笔记本电脑上,如果我从ScatterplotTestSpeed.py运行示例代码,则获得10k点的更新速度为0.16 fps。但是,如果我将列表添加为元组,我可以将更新速度增加三倍,达到0.45 fps:

colors_=[]
for i in colors:
  colors_.append(pg.mkBrush(tuple(i)))

这是我通过简单的尝试和看到方法发现的一个小技巧,但我想知道是否有更多这样的技巧来加速fps数量?!

这是我测试更新速度的完整代码。它主要基于ScatterPlotSpeedTest.py形式的pyqtgraphs示例库。任何帮助表示赞赏: - )

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
For testing rapid updates of ScatterPlotItem under various conditions.

(Scatter plots are still rather slow to draw; expect about 20fps)
"""



## Add path to library (just for examples; you do not need this)
import initExample

import matplotlib as mpl
import matplotlib.cm as cm
from pyqtgraph.Qt import QtGui, QtCore, USE_PYSIDE
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
#QtGui.QApplication.setGraphicsSystem('raster')
app = QtGui.QApplication([])
#mw = QtGui.QMainWindow()
#mw.resize(800,800)
if USE_PYSIDE:
    from ScatterPlotSpeedTestTemplate_pyside import Ui_Form
else:
    from ScatterPlotSpeedTestTemplate_pyqt import Ui_Form

win = QtGui.QWidget()
win.setWindowTitle('pyqtgraph example: ScatterPlotSpeedTest')
ui = Ui_Form()
ui.setupUi(win)
win.show()

p = ui.plot
p.setRange(xRange=[-500, 500], yRange=[-500, 500])

data = np.random.normal(size=(50,10000), scale=100)
sizeArray = (np.random.random(500) * 20.).astype(int)
ptr = 0
lastTime = time()
fps = None
def update():
    global curve, data, ptr, p, lastTime, fps
    p.clear()
    if ui.randCheck.isChecked():
        size = sizeArray
    else:
        size = ui.sizeSpin.value()


    z=np.random.randint(0,10000,10000)
    norm = mpl.colors.Normalize(vmin=min(z), vmax=max(z))
    m = cm.ScalarMappable(norm=norm, cmap=cm.jet)
    colors =  m.to_rgba(z, bytes=True)

    colors_=[]
    for i in colors:
      colors_.append(pg.mkBrush(tuple(i)))
      #colors.append(pg.intColor(np.random.randint(0,255), 100))

    curve = pg.ScatterPlotItem(x=data[ptr%50], y=data[(ptr+1)%50],
                   pen='w', brush=colors_, size=size,
                   pxMode=ui.pixelModeCheck.isChecked())


    '''
    curve = pg.ScatterPlotItem(pen='w', size=size, pxMode=ui.pixelModeCheck.isChecked())
    spots3=[]

    for i,j,k in zip(data[ptr%50],data[(ptr+1)%50],colors):
      spots3.append({'pos': (i, j), 'brush':pg.mkBrush(tuple(k))})

    curve.addPoints(spots3)
    '''



    p.addItem(curve)
    ptr += 1
    now = time()
    dt = now - lastTime
    lastTime = now
    if fps is None:
        fps = 1.0/dt
    else:
        s = np.clip(dt*3., 0, 1)
        fps = fps * (1-s) + (1.0/dt) * s
    p.setTitle('%0.2f fps' % fps)
    p.repaint()
    #app.processEvents()  ## force complete redraw for every plot
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)



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

1 个答案:

答案 0 :(得分:1)

我认为我找到了解决问题的方法。似乎ScatterPlotItem太慢而不能用'brush'指定的不同颜色连续重绘大量的点(> 500)。相反,我只是使用plotItem和'symbolBrush'来为Jet颜色着色输入数据点(这将给出我最初搜索的2.5D图)。无论添加多少数据点,绘图更新的速度几乎是即时的,100点重新着色的速度与10000点一样快(从用户的角度来看)。