forked from M-Labs/artiq
1
0
Fork 0

applets/plot_xy_hist: use applets.simple and datasets

This commit is contained in:
Sebastien Bourdeauducq 2016-01-13 05:52:33 -07:00
parent 341bbdee6b
commit e37e0bdc1c
1 changed files with 51 additions and 36 deletions

View File

@ -1,69 +1,84 @@
#!/usr/bin/env python3.5 #!/usr/bin/env python3.5
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np import numpy as np
import pyqtgraph
class XYHistPlot: from artiq.applets.simple import SimpleApplet
def __init__(self):
self.graphics_window = pg.GraphicsWindow(title="XY/Histogram")
self.graphics_window.resize(1000,600)
self.graphics_window.setWindowTitle("XY/Histogram")
self.xy_plot = self.graphics_window.addPlot()
class XYHistPlot(pyqtgraph.GraphicsWindow):
def __init__(self, args):
pyqtgraph.GraphicsWindow.__init__(self, title="XY/Histogram")
self.resize(1000,600)
self.setWindowTitle("XY/Histogram")
self.xy_plot = self.addPlot()
self.xy_plot_data = None self.xy_plot_data = None
self.arrow = None self.arrow = None
self.hist_plot = self.graphics_window.addPlot() self.hist_plot = self.addPlot()
self.hist_plot_data = None self.hist_plot_data = None
def set_data(self, xs, histograms_bins, histograms_counts): self.args = args
ys = np.empty_like(xs)
ys.fill(np.nan) def _set_full_data(self, xs, histogram_bins, histograms_counts):
for n, (bins, counts) in enumerate(zip(histograms_bins, self.xy_plot.clear()
histograms_counts)): self.hist_plot.clear()
bin_centers = np.empty(len(bins)-1) self.xy_plot_data = None
self.hist_plot_data = None
self.arrow = None
self.histogram_bins = histogram_bins
bin_centers = np.empty(len(histogram_bins)-1)
for i in range(len(bin_centers)): for i in range(len(bin_centers)):
bin_centers[i] = (bins[i] + bins[i+1])/2 bin_centers[i] = (histogram_bins[i] + histogram_bins[i+1])/2
ys[n] = sum(bin_centers*counts)/sum(bin_centers)
ys = np.empty_like(xs)
for n, counts in enumerate(histograms_counts):
ys[n] = sum(bin_centers*counts)/sum(counts)
self.xy_plot_data = self.xy_plot.plot(x=xs, y=ys, self.xy_plot_data = self.xy_plot.plot(x=xs, y=ys,
pen=None, pen=None,
symbol="x", symbolSize=20) symbol="x", symbolSize=20)
self.xy_plot_data.sigPointsClicked.connect(self.point_clicked) self.xy_plot_data.sigPointsClicked.connect(self._point_clicked)
for point, bins, counts in zip(self.xy_plot_data.scatter.points(), for point, counts in zip(self.xy_plot_data.scatter.points(),
histograms_bins, histograms_counts): histograms_counts):
point.histogram_bins = bins
point.histogram_counts = counts point.histogram_counts = counts
self.hist_plot_data = self.hist_plot.plot( self.hist_plot_data = self.hist_plot.plot(
stepMode=True, fillLevel=0, stepMode=True, fillLevel=0,
brush=(0, 0, 255, 150)) brush=(0, 0, 255, 150))
def point_clicked(self, data_item, spot_items): def _point_clicked(self, data_item, spot_items):
spot_item = spot_items[0] spot_item = spot_items[0]
position = spot_item.pos() position = spot_item.pos()
if self.arrow is None: if self.arrow is None:
self.arrow = pg.ArrowItem(angle=-120, tipAngle=30, baseAngle=20, self.arrow = pyqtgraph.ArrowItem(
headLen=40, tailLen=40, tailWidth=8, angle=-120, tipAngle=30, baseAngle=20, headLen=40,
pen=None, brush="y") tailLen=40, tailWidth=8, pen=None, brush="y")
self.arrow.setPos(position) self.arrow.setPos(position)
# NB: temporary glitch if addItem is done before setPos # NB: temporary glitch if addItem is done before setPos
self.xy_plot.addItem(self.arrow) self.xy_plot.addItem(self.arrow)
else: else:
self.arrow.setPos(position) self.arrow.setPos(position)
self.hist_plot_data.setData(x=spot_item.histogram_bins, self.hist_plot_data.setData(x=self.histogram_bins,
y=spot_item.histogram_counts) y=spot_item.histogram_counts)
def data_changed(self, data, mods):
xs = data[self.args.xs][1]
histogram_bins = data[self.args.histogram_bins][1]
histograms_counts = data[self.args.histograms_counts][1]
self._set_full_data(xs, histogram_bins, histograms_counts)
def main(): def main():
app = QtGui.QApplication([]) applet = SimpleApplet(XYHistPlot)
plot = XYHistPlot() applet.add_dataset("xs", "1D array of point abscissas")
plot.set_data(np.array([1, 2, 3, 4, 1]), applet.add_dataset("histogram_bins",
np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3], [40, 70, 100], [4, 7, 10, 20]]), "1D array of histogram bin boundaries")
np.array([[1, 1], [2, 3], [10, 20], [3, 1], [100, 67, 102]])) applet.add_dataset("histograms_counts",
app.exec_() "2D array of histogram counts, for each point")
applet.run()
if __name__ == '__main__': if __name__ == "__main__":
main() main()