以下是使用 pyqtgraph
實作的高效能 100Hz IMU 即時繪圖 GUI,支援:
✅ 功能特色
功能 | 說明 |
---|---|
⚡ 高速更新 | 使用 pyqtgraph 支援 100Hz 頻率流暢更新 |
🎨 即時圖表繪製 | 即時顯示 b1~b6 任意組合資料 |
🎛 資料組別切換 | 可選擇顯示 b1~b6 / b1~b3 / b4~b6 |
⏱ 時間窗可設定 | 顯示 1~5 秒 IMU 時域資料 |
📦 安裝 pyqtgraph
bashpip install pyqtgraph
🧠 完整 PyQt5 + pyqtgraph 即時繪圖程式
pythonimport sys
import pandas as pd
import numpy as np
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QFileDialog, QComboBox, QLabel
)
from PyQt5.QtCore import QTimer
import pyqtgraph as pg
class IMUGraph(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("IMU 即時繪圖 (100Hz) - pyqtgraph")
self.resize(1000, 600)
# 資料相關
self.data = None
self.index = 0
self.fs = 100 # 100Hz
self.seq_len = 100
self.group_mode = 0 # b1~b6
# 定時器
self.timer = QTimer()
self.timer.timeout.connect(self.update_plot)
# UI
self.layout = QVBoxLayout()
self.setLayout(self.layout)
control_layout = QHBoxLayout()
self.load_button = QPushButton("📂 載入 CSV")
self.play_button = QPushButton("▶️ 播放 / 暫停")
self.combo_time = QComboBox()
self.combo_time.addItems(["1 秒", "2 秒", "3 秒", "4 秒", "5 秒"])
self.combo_group = QComboBox()
self.combo_group.addItems(["顯示 b1~b6", "顯示 b1~b3", "顯示 b4~b6"])
self.label = QLabel("📌 尚未載入資料")
control_layout.addWidget(self.load_button)
control_layout.addWidget(self.play_button)
control_layout.addWidget(QLabel("時間窗:"))
control_layout.addWidget(self.combo_time)
control_layout.addWidget(QLabel("資料組:"))
control_layout.addWidget(self.combo_group)
self.layout.addLayout(control_layout)
self.layout.addWidget(self.label)
# 繪圖區
self.plot_widget = pg.PlotWidget(title="IMU 即時資料")
self.plot_widget.setLabel("bottom", "時間", "秒")
self.plot_widget.setLabel("left", "數值")
self.layout.addWidget(self.plot_widget)
self.curves = []
self.colors = ['r', 'g', 'b']
# 綁定事件
self.load_button.clicked.connect(self.load_csv)
self.play_button.clicked.connect(self.toggle_play)
self.combo_time.currentIndexChanged.connect(self.set_seq_len)
self.combo_group.currentIndexChanged.connect(self.set_group)
def load_csv(self):
fname, _ = QFileDialog.getOpenFileName(self, "選擇 CSV 檔", "", "CSV files (*.csv)")
if fname:
self.data = pd.read_csv(fname, encoding='utf-8')
self.label.setText(f"✅ 已載入:{fname.split('/')[-1]}")
self.index = 0
self.set_seq_len()
self.set_group()
self.init_plot()
def set_seq_len(self):
self.seq_len = self.fs * (self.combo_time.currentIndex() + 1)
def set_group(self):
self.group_mode = self.combo_group.currentIndex()
self.init_plot()
def init_plot(self):
self.plot_widget.clear()
self.curves = []
if self.group_mode == 0: # b1~b6
for i in range(6):
curve = self.plot_widget.plot(pen=pg.mkPen(self.colors[i % 3], width=2, style=pg.QtCore.Qt.SolidLine if i < 3 else pg.QtCore.Qt.DashLine))
self.curves.append(curve)
elif self.group_mode == 1: # b1~b3
for i in range(3):
curve = self.plot_widget.plot(pen=pg.mkPen(self.colors[i], width=2))
self.curves.append(curve)
elif self.group_mode == 2: # b4~b6
for i in range(3):
curve = self.plot_widget.plot(pen=pg.mkPen(self.colors[i], width=2))
self.curves.append(curve)
def toggle_play(self):
if self.timer.isActive():
self.timer.stop()
self.play_button.setText("▶️ 播放 / 暫停")
else:
self.timer.start(10) # 100Hz
self.play_button.setText("⏸️ 播放中...")
def update_plot(self):
if self.data is None or self.index + self.seq_len > len(self.data):
self.timer.stop()
self.play_button.setText("▶️ 播放 / 暫停")
return
segment = self.data.iloc[self.index:self.index + self.seq_len]
time = np.linspace(0, self.seq_len / self.fs, self.seq_len)
if self.group_mode == 0: # b1~b6
for i in range(6):
self.curves[i].setData(time, segment[f'b{i+1}'].values)
elif self.group_mode == 1: # b1~b3
for i in range(3):
self.curves[i].setData(time, segment[f'b{i+1}'].values)
elif self.group_mode == 2: # b4~b6
for i in range(3):
self.curves[i].setData(time, segment[f'b{i+4}'].values)
self.index += 1
if __name__ == "__main__":
app = QApplication(sys.argv)
gui = IMUGraph()
gui.show()
sys.exit(app.exec_())
✅ 效能特點
比較 | matplotlib | pyqtgraph |
---|---|---|
每秒重繪速率 | 約 10~30 fps | 最高可達數百 fps |
即時顯示延遲 | 明顯延遲 | 幾乎即時 |
適合用途 | 靜態分析 | 即時 IMU、EEG、ECG 等資料流 |
需要我幫你進一步加入:
-
顯示
type
分類標籤? -
即時套用分類模型(LSTM)預測並顯示?
-
儲存播放結果為影片或 CSV?
沒有留言:
張貼留言