我可以使用pyqtgraph来绘制实时串行数据流吗?

问题描述 投票:0回答:1

我正在尝试通过树莓派上的 USB/串行接口绘制来自距离计传感器的 10-100Hz 数据。 我可以这样读取数据:

value = ser.readline().decode('utf-8')
data = value.split(",")
distance = data[1]

我正在尝试调整在 pythonguis.com 上找到的以下代码,但我不知道如何调整“update_plot”(和 init)以读取和绘制我的数据流。 你最终能教我怎么做吗?

from random import randint

import pyqtgraph as pg
from PyQt5 import QtCore, QtWidgets

class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
    super().__init__()

    # Temperature vs time dynamic plot
    self.plot_graph = pg.PlotWidget()
    self.setCentralWidget(self.plot_graph)
    self.plot_graph.setBackground("w")
    pen = pg.mkPen(color=(255, 0, 0))
    self.plot_graph.setTitle("Temperature vs Time", color="b", size="20pt")
    styles = {"color": "red", "font-size": "18px"}
    self.plot_graph.setLabel("left", "Temperature (°C)", **styles)
    self.plot_graph.setLabel("bottom", "Time (min)", **styles)
    self.plot_graph.addLegend()
    self.plot_graph.showGrid(x=True, y=True)
    self.plot_graph.setYRange(20, 40)
    self.time = list(range(10))
    self.temperature = [randint(20, 40) for _ in range(10)]
    # Get a line reference
    self.line = self.plot_graph.plot(
        self.time,
        self.temperature,
        name="Temperature Sensor",
        pen=pen,
        symbol="+",
        symbolSize=15,
        symbolBrush="b",
    )
    # Add a timer to simulate new temperature measurements
    self.timer = QtCore.QTimer()
    self.timer.setInterval(300)
    self.timer.timeout.connect(self.update_plot)
    self.timer.start()

def update_plot(self):
    self.time = self.time[1:]
    self.time.append(self.time[-1] + 1)
    self.temperature = self.temperature[1:]
    self.temperature.append(randint(20, 40))
    self.line.setData(self.time, self.temperature)

app = QtWidgets.QApplication([])
main = MainWindow()
main.show()
app.exec()

谢谢

plot stream pyqt5 pyqtgraph
1个回答
0
投票

matplotlib 库可能更适合您

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.