2025-03-05 10:27:27 +01:00

94 lines
3.0 KiB
Python

import pyqtgraph as pg
from PyQt6.QtWidgets import QWidget
class Plot(pg.GraphicsLayoutWidget):
"""
pyqtgraph plot widget for showing voltage and LED vs time
"""
def __init__(self):
super().__init__()
self.setWindowTitle("CPD - LED")
# self.x_axis.setLabel("Time [s]")
# self.v_axis.setLabel("CPD [V]")
# self.l_axis.setLabel("LED [%]")
# self.layout.addItem(self.x_axis, row=1, col=1, rowspan=1, colspan=1)
# self.layout.addItem(self.v_axis, row=1, col=1, rowspan=1, colspan=1)
self.v_plot_item = pg.PlotItem()
self.v_box = self.v_plot_item.vb
self.addItem(self.v_plot_item, row=1, col=1)
self.v_plot_item.showAxis('right')
self.v_plot_item.setLabel('right', "LED [%]")
self.v_plot_item.setLabel('left', "CPD [V]")
self.v_plot_item.setLabel('bottom', "time [s]")
self.l_box = pg.ViewBox()
self.v_plot_item.scene().addItem(self.l_box)
self.v_plot_item.getAxis('right').linkToView(self.l_box)
self.l_box.setXLink(self.v_plot_item)
self.l_box.setYRange(0, 110)
self.setBackground("w")
# self.setTitle("Test")
# self.setLabel("bottom", "time [s]")
# self.setLabel("left", "Voltage [V]")
# self.setLabel("right", "LED [%]")
# self.getAxis("right").setRange(0, 110) # Adding some margin
# self.showGrid(x=True, y=True)
self.time = []
self.voltage = []
self.led = []
self.v_line = pg.PlotCurveItem(
self.time,
self.voltage,
pen=pg.mkPen("b", width=2),
symbol="o",
symbolSize=5,
symbolBrush="b",
)
self.l_line = pg.PlotCurveItem(
self.time,
self.led,
pen=pg.mkPen("r", width=2)
)
self.l_box.addItem(self.l_line)
self.v_box.addItem(self.v_line)
self.update_views()
self.v_plot_item.getViewBox().sigResized.connect(self.update_views)
def update_views(self):
"""
Make sure the linked view boxes have the correct size
(called after resizing of the plot)
"""
self.l_box.setGeometry(self.v_plot_item.getViewBox().sceneBoundingRect())
self.l_box.linkedViewChanged(self.v_plot_item.getViewBox(), self.l_box.XAxis)
def update_plot(self, time, voltage, led):
"""
Add new data points to the plot
Parameters
----------
time
voltage
led
"""
self.time.append(time)
self.voltage.append(voltage)
self.led.append(led)
self.v_line.setData(self.time, self.voltage)
self.l_line.setData(self.time, self.led)
def clear_data(self):
"""
Clear the lines and data
Returns
"""
self.time = []
self.voltage = []
self.led = []
self.v_line.setData(self.time, self.voltage)
self.l_line.setData(self.time, self.led)