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

时间:2014-04-23 09:57:01

标签: python arrays qt numpy pyqtgraph

我在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 个答案:

答案 0 :(得分: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')