admin 發表於 2023-4-24 15:20:59

PyQtGraph plotting over time

from PyQt5 import QtWidgets, QtCore
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys# We need sys so that we can pass argv to QApplication
import os
from random import randint

class MainWindow(QtWidgets.QMainWindow):

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

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

      self.x = list(range(100))# 100 time points
      self.y = # 100 data points

      self.graphWidget.setBackground('w')

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

      self.timer = QtCore.QTimer()
      self.timer.setInterval(50)
      self.timer.timeout.connect(self.update_plot_data)
      self.timer.start()

    def update_plot_data(self):

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

      self.y = self.y# Remove the first
      self.y.append( randint(0,100))# Add a new random value.

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


app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())Matplotlib 轉移至 PyQtGraph 製作Real Time Curve
頁: [1]
查看完整版本: PyQtGraph plotting over time