gpt4 book ai didi

python - 使用 pyqtgraph 实时绘制许多子图

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:52 25 4
gpt4 key购买 nike

我用了this问题并尝试使用它来快速实时绘制许多子图。

不幸的是,我很难理解代码,因此我无法根据需要更改它。

我想创建一个 2x2 矩阵的子图,像素为 10x10。现在我得到以下信息:

enter image description here

代码如下所示:

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

class App(QtGui.QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)

#### Create Gui Elements ###########
self.mainbox = QtGui.QWidget()
self.setCentralWidget(self.mainbox)
self.mainbox.setLayout(QtGui.QVBoxLayout())

self.canvas = pg.GraphicsLayoutWidget()
self.mainbox.layout().addWidget(self.canvas)

self.label = QtGui.QLabel()
self.mainbox.layout().addWidget(self.label)

self.view = self.canvas.addViewBox()
self.view.setAspectLocked(True)
self.view.setRange(QtCore.QRectF(0, 0, 50, 50))

self.img = []
for i in range(4):
self.img.append(pg.ImageItem(None, border="w"))
self.canvas.nextRow()
self.view.addItem(self.img[i])

self._update()

def _update(self):
for i in range(4):
self.data = np.random.rand(10,10)
self.img[i].setImage(self.data)

QtCore.QTimer.singleShot(1, self._update)

def sensor_data(n_sensors, x_res, y_res):
return np.random.rand(n_sensors, x_res, y_res)

if __name__ == '__main__':
while True:
# Get sensor data
data = sensor_data(4, 10, 10)
# Pass data to live plot function?

app = QtGui.QApplication(sys.argv)
thisapp = App()
thisapp.show()
sys.exit(app.exec_())

有人可以告诉我我做错了什么吗?

最佳答案

您只构建了一个 ViewBox 并在其中添加了项目,这导致了问题,您必须创建多个 ViewBox 并添加一个项目,如下所示:

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


class App(QtGui.QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
self.mainbox = QtGui.QWidget()
self.setCentralWidget(self.mainbox)

self.canvas = pg.GraphicsLayoutWidget()
self.label = QtGui.QLabel()

lay = QtGui.QVBoxLayout(self.mainbox)
lay.addWidget(self.canvas)
lay.addWidget(self.label)

self.img_items = []

for i in range(4):
view = self.canvas.addViewBox()
view.setAspectLocked(True)
view.setRange(QtCore.QRectF(0, 0, 10, 10))
it = pg.ImageItem(None, border="w")
view.addItem(it)
self.img_items.append(it)
self.canvas.nextRow()

timer = QtCore.QTimer(self, interval=1)
timer.timeout.connect(self._update)
timer.start()

def _update(self):
for item in self.img_items:
data = np.random.rand(10, 10)
item.setImage(data)

if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
thisapp = App()
thisapp.show()
sys.exit(app.exec_())

enter image description here

更新:

N×N

n = 2
for i in range(n):
for j in range(n):
view = self.canvas.addViewBox(i, j)
view.setAspectLocked(True)
view.setRange(QtCore.QRectF(0, 0, 10, 10))
it = pg.ImageItem(None, border="w")
view.addItem(it)
self.img_items.append(it)

enter image description here

更新:

如果你想在一段时间内获取数据 True 你必须在一个新的线程中进行,以避免 GUI 被阻塞,你还必须稍微休眠以便 GUI 可以更新:

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


def sensor_data(n_sensors, x_res, y_res):
return np.random.rand(n_sensors, x_res, y_res)


class Thread(QtCore.QThread):
dataChanged = QtCore.pyqtSignal(np.ndarray)
def run(self):
while True:
data = sensor_data(4, 10, 10)
self.dataChanged.emit(data)
QtCore.QThread.msleep(10)


class App(QtGui.QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
self.mainbox = QtGui.QWidget()
self.setCentralWidget(self.mainbox)

self.canvas = pg.GraphicsLayoutWidget()
self.label = QtGui.QLabel()

lay = QtGui.QVBoxLayout(self.mainbox)
lay.addWidget(self.canvas)
lay.addWidget(self.label)

self.img_items = []
n = 2

for i in range(n):
for j in range(n):
view = self.canvas.addViewBox(i, j)
view.setAspectLocked(True)
view.setRange(QtCore.QRectF(0, 0, 10, 10))
it = pg.ImageItem(None, border="w")
view.addItem(it)
self.img_items.append(it)

@QtCore.pyqtSlot(np.ndarray)
def update_data(self, data):
for i, v in enumerate(data):
self.img_items[i].setImage(v)


if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
thisapp = App()
thread = Thread()
thread.dataChanged.connect(thisapp.update_data)
thread.start()
thisapp.show()
sys.exit(app.exec_())

关于python - 使用 pyqtgraph 实时绘制许多子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53032042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com