2017-01-30 09:24:43 +08:00
|
|
|
#!/usr/bin/env python3
|
2016-01-10 18:19:34 +08:00
|
|
|
|
|
|
|
import numpy as np
|
2016-02-09 05:25:20 +08:00
|
|
|
import PyQt5 # make sure pyqtgraph imports Qt5
|
2016-01-10 18:19:34 +08:00
|
|
|
import pyqtgraph
|
|
|
|
|
2016-04-06 17:08:38 +08:00
|
|
|
from artiq.applets.simple import TitleApplet
|
2016-01-10 18:19:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
class XYPlot(pyqtgraph.PlotWidget):
|
|
|
|
def __init__(self, args):
|
|
|
|
pyqtgraph.PlotWidget.__init__(self)
|
|
|
|
self.args = args
|
|
|
|
|
2016-04-06 17:08:38 +08:00
|
|
|
def data_changed(self, data, mods, title):
|
2016-01-10 18:19:34 +08:00
|
|
|
try:
|
|
|
|
y = data[self.args.y][1]
|
|
|
|
except KeyError:
|
|
|
|
return
|
|
|
|
x = data.get(self.args.x, (False, None))[1]
|
|
|
|
if x is None:
|
2016-05-31 11:48:32 +08:00
|
|
|
x = np.arange(len(y))
|
2016-01-10 18:19:34 +08:00
|
|
|
error = data.get(self.args.error, (False, None))[1]
|
|
|
|
fit = data.get(self.args.fit, (False, None))[1]
|
|
|
|
|
|
|
|
if not len(y) or len(y) != len(x):
|
|
|
|
return
|
|
|
|
if error is not None and hasattr(error, "__len__"):
|
|
|
|
if not len(error):
|
|
|
|
error = None
|
|
|
|
elif len(error) != len(y):
|
|
|
|
return
|
|
|
|
if fit is not None:
|
|
|
|
if not len(fit):
|
|
|
|
fit = None
|
|
|
|
elif len(fit) != len(y):
|
|
|
|
return
|
|
|
|
|
|
|
|
self.clear()
|
|
|
|
self.plot(x, y, pen=None, symbol="x")
|
2016-04-06 17:08:38 +08:00
|
|
|
self.setTitle(title)
|
2016-01-10 18:19:34 +08:00
|
|
|
if error is not None:
|
|
|
|
# See https://github.com/pyqtgraph/pyqtgraph/issues/211
|
|
|
|
if hasattr(error, "__len__") and not isinstance(error, np.ndarray):
|
|
|
|
error = np.array(error)
|
2016-04-08 01:21:59 +08:00
|
|
|
errbars = pyqtgraph.ErrorBarItem(
|
|
|
|
x=np.array(x), y=np.array(y), height=error)
|
2016-01-10 18:19:34 +08:00
|
|
|
self.addItem(errbars)
|
|
|
|
if fit is not None:
|
2016-04-08 01:17:22 +08:00
|
|
|
xi = np.argsort(x)
|
|
|
|
self.plot(x[xi], fit[xi])
|
2016-01-10 18:19:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2016-04-06 17:08:38 +08:00
|
|
|
applet = TitleApplet(XYPlot)
|
2016-01-10 18:19:34 +08:00
|
|
|
applet.add_dataset("y", "Y values")
|
|
|
|
applet.add_dataset("x", "X values", required=False)
|
|
|
|
applet.add_dataset("error", "Error bars for each X value", required=False)
|
|
|
|
applet.add_dataset("fit", "Fit values for each X value", required=False)
|
|
|
|
applet.run()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|