艾歐踢論壇

標題: PyQtGraph plotting over time [打印本頁]

作者: admin    時間: 2023-4-24 15:20
標題: PyQtGraph plotting over time
  1. from PyQt5 import QtWidgets, QtCore
  2. from pyqtgraph import PlotWidget, plot
  3. import pyqtgraph as pg
  4. import sys  # We need sys so that we can pass argv to QApplication
  5. import os
  6. from random import randint

  7. class MainWindow(QtWidgets.QMainWindow):

  8.     def __init__(self, *args, **kwargs):
  9.         super(MainWindow, self).__init__(*args, **kwargs)

  10.         self.graphWidget = pg.PlotWidget()
  11.         self.setCentralWidget(self.graphWidget)

  12.         self.x = list(range(100))  # 100 time points
  13.         self.y = [randint(0,100) for _ in range(100)]  # 100 data points

  14.         self.graphWidget.setBackground('w')

  15.         pen = pg.mkPen(color=(255, 0, 0))
  16.         self.data_line =  self.graphWidget.plot(self.x, self.y, pen=pen)

  17.         self.timer = QtCore.QTimer()
  18.         self.timer.setInterval(50)
  19.         self.timer.timeout.connect(self.update_plot_data)
  20.         self.timer.start()

  21.     def update_plot_data(self):

  22.         self.x = self.x[1:]  # Remove the first y element.
  23.         self.x.append(self.x[-1] + 1)  # Add a new value 1 higher than the last.

  24.         self.y = self.y[1:]  # Remove the first
  25.         self.y.append( randint(0,100))  # Add a new random value.

  26.         self.data_line.setData(self.x, self.y)  # Update the data.


  27. app = QtWidgets.QApplication(sys.argv)
  28. w = MainWindow()
  29. w.show()
  30. sys.exit(app.exec_())
複製代碼
Matplotlib 轉移至 PyQtGraph 製作Real Time Curve





歡迎光臨 艾歐踢論壇 (http://www.iot.idv.tw/ucenter/) Powered by Discuz! X3.2