调整大小后pyqtgraph实时图冻结

时间:2015-01-28 20:19:58

标签: python pyqtgraph

我在pyqtgraph中绘制通过UDP接收的实时数据。一切都很好,每次更新2048点,但有4096或8k点,一旦我尝试调整大小或放大,情节就会冻结。如果我切换logY刻度,则更新恢复。我已经验证UDP流中没有间隙。也尝试使用

# p.setDownsampling(auto=True)
# p.setClipToView(True)

选项,但他们没有帮助

我已经读过pyqtgraph可以处理比这更多的数据,所以我有点困惑。这个问题可以在这里看到:https://www.youtube.com/watch?v=iL-WHEbSx-w

以下是代码,我直接从示例中直接进行了修改

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time

from struct import pack, unpack

from socket import *

app = QtGui.QApplication([])

data = []

p = pg.plot(fillLevel=20)
p.setWindowTitle('pyqtgraph example: PlotSpeedTest')
p.setRange(QtCore.QRectF(0, 0, 30720000, 10 )) 
p.setLabel('bottom', 'Frequency', units='Hz')
p.showGrid(True, True)
fScale = np.linspace(0, 30720000, 8192)

# p.setDownsampling(auto=True)
# p.setClipToView(True)

curve = p.plot()

# Receive UDP packets transmitted by a broadcasting service

sock = socket(AF_INET, SOCK_DGRAM)
try:
        sock.bind(('', 1234))
except:
        print "Error connecting to the UDP stream."
        quit


ptr = 0
lastTime = time()
fps = None
def update():
    global sock, fScale
    global curve, data, ptr, p, lastTime, fps
    stream = sock.recv(16384)
    if (len(stream) == 16384):
        data_float16 = np.fromstring(stream, dtype=np.float16)
        data = data_float16.astype(np.float32)
        print "got data", lastTime, "\n"
    # print data
    curve.setData(fScale,data,_callSync='off')
    # curve.setData(data,_callSync='off')
    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)
    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 :(得分:2)

如果数据中有任何Inf,则会混淆确定是否重绘图的边界矩形计算。首先尝试清理数据:

data[np.isinf(data)] = np.nan