0

I'm trying to plot 10-100Hz datas coming from a distance meter sensor over a USB/serial interface on a raspberry. I can read datas like this: value = ser.readline().decode('utf-8') data = value.split(",") distance = data[1]

I'm trying to adapt the following code found on pythonguis.com but I can't figure out how to adapt the "update_plot" (and init) in order to read and plot my data stream. Could you eventually teach me how to do that ?

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()

Thanks

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.