artiq/artiq/applets/plot_hist.py

55 lines
1.5 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
2016-01-10 18:23:31 +08:00
import PyQt5 # make sure pyqtgraph imports Qt5
from PyQt5.QtCore import QTimer
2016-01-10 18:23:31 +08:00
import pyqtgraph
from artiq.applets.simple import TitleApplet
2016-01-10 18:23:31 +08:00
class HistogramPlot(pyqtgraph.PlotWidget):
def __init__(self, args, req):
2016-01-10 18:23:31 +08:00
pyqtgraph.PlotWidget.__init__(self)
self.args = args
self.timer = QTimer()
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.length_warning)
2016-01-10 18:23:31 +08:00
2023-07-12 12:04:22 +08:00
def data_changed(self, value, metadata, persist, mods, title):
2016-01-10 18:23:31 +08:00
try:
2023-07-12 12:04:22 +08:00
y = value[self.args.y]
2016-01-10 18:23:31 +08:00
if self.args.x is None:
x = None
else:
2023-07-12 12:04:22 +08:00
x = value[self.args.x]
2016-01-10 18:23:31 +08:00
except KeyError:
return
if x is None:
x = list(range(len(y)+1))
if len(y) and len(x) == len(y) + 1:
self.timer.stop()
2016-01-10 18:23:31 +08:00
self.clear()
self.plot(x, y, stepMode=True, fillLevel=0,
brush=(0, 0, 255, 150))
self.setTitle(title)
else:
2021-07-19 12:26:01 +08:00
if not self.timer.isActive():
self.timer.start(1000)
def length_warning(self):
self.clear()
text = "⚠️ dataset lengths mismatch:\n"\
"There should be one more bin boundaries than there are Y values"
self.addItem(pyqtgraph.TextItem(text))
2016-01-10 18:23:31 +08:00
def main():
applet = TitleApplet(HistogramPlot)
2016-01-10 18:23:31 +08:00
applet.add_dataset("y", "Y values")
2016-01-16 03:50:09 +08:00
applet.add_dataset("x", "Bin boundaries", required=False)
2016-01-10 18:23:31 +08:00
applet.run()
if __name__ == "__main__":
main()