如何在pyqtgraph的remoteview中绘制切片的numpy数据数组

0 投票
1 回答
1264 浏览
提问于 2025-04-18 03:54

我在使用PyQTGraph中的RemoteGraphicsView()功能时遇到了问题。我有一个numpy的ndarray(就是一种数据结构),我想在RemoteGraphicsView上绘制它(这样做是为了速度,因为它在一个单独的进程中运行)。我想绘制数据的一部分,但结果出现了一个类型错误(TypeError)。

TypeError: must be string or single-segment read-only buffer, not numpy.ndarray

下面是一个代码片段,展示了我在一个更大程序中遇到的问题。可以看到,数据是一个numpy.ndarray,并且我对它进行了切片处理。

import pyqtgraph as pg
import numpy as np
import pyqtgraph.widgets.RemoteGraphicsView

app = pg.mkQApp()

datview = pg.widgets.RemoteGraphicsView.RemoteGraphicsView(debug=False)
datview.pg.setConfigOptions(antialias=True) 
allplots = datview.pg.GraphicsLayout()
datview.setCentralItem(allplots)

w1 = allplots.addPlot(row=0,col=0)

layoutWG = pg.LayoutWidget()
layoutWG.addWidget(datview)
layoutWG.show()

data = np.random.randn(10000,100)
curve1 = w1.plot(pen='r')

now = pg.ptime.time()
for n in range(100):
    curve1.setData(data[:,n])
    app.processEvents()

app.exec_()

任何帮助都将非常感谢。

1 个回答

1

看起来进程间通信系统需要一个连续的数组。这段代码应该可以正常工作:

    curve1.setData(np.ascontiguousarray(data[:,n]))

另外,你可以定义数据,使得你想要的切片(数据片段)已经是连续的:

data = np.random.randn(100,10000)
...
for n in range(100):
    curve1.setData(data[n])

我还建议做几个改动来加快速度:

# Prevent lookups to curve1.setData from immediately requesting and returning
# the method proxy every time it is called
curve1._setProxyOptions(deferGetattr=True)

for n in range(100):
    # Use _callSync='off' to prevent the main process waiting for a return 
    # value
    curve1.setData(np.ascontiguousarray(data[:,n]), _callSync='off')

撰写回答