以下是使用 PySide2 和 pyqtgraph 实现实时刷新的直方图的示例代码:\n\npython\nfrom PySide2.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QVBoxLayout, QWidget\nimport pyqtgraph as pg\nimport numpy as np\nimport random\nimport time\n\nclass MainWindow(QMainWindow):\n def __init__(self):\n super().__init__()\n\n self.setWindowTitle("Real-time Histogram")\n\n # Create a graphics view and scene\n self.view = QGraphicsView()\n self.scene = QGraphicsScene()\n self.view.setScene(self.scene)\n\n # Create a layout and add the graphics view to it\n layout = QVBoxLayout()\n layout.addWidget(self.view)\n\n # Create a central widget and set the layout\n central_widget = QWidget()\n central_widget.setLayout(layout)\n self.setCentralWidget(central_widget)\n\n # Create a histogram item and add it to the scene\n self.hist = pg.HistogramLUTItem()\n self.hist.setImageItem(pg.ImageItem())\n self.scene.addItem(self.hist)\n\n # Set up the data\n self.data = np.zeros((100, 100))\n self.hist.setImage(self.data)\n\n # Start the timer for real-time updates\n self.timer = self.startTimer(100)\n\n def timerEvent(self, event):\n # Update the data with random values\n for i in range(10):\n x = random.randint(0, 99)\n y = random.randint(0, 99)\n self.data[x, y] += 1\n\n # Update the histogram\n self.hist.setImage(self.data)\n\nif __name__ == "__main__":\n app = QApplication([])\n window = MainWindow()\n window.show()\n app.exec_()\n\n\n这个示例代码创建了一个实时更新的直方图窗口,在每个100毫秒的时间间隔内随机更新直方图数据。图像可以通过鼠标拖动和缩放进行交互。\n\n

PySide2 和 PyQtGraph 实现实时更新直方图 - 示例代码

原文地址: https://www.cveoy.top/t/topic/oIV3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录