-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram_widget.py
116 lines (83 loc) · 2.97 KB
/
histogram_widget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python2.5
#
# Written (W) 2011-2014 Christian Widmer
# Copyright (C) 2011-2014 Max-Planck-Society, MSKCC, TU-Berlin
"""
@author: Christian Widmer
@summary: Interactive histogram using matplotlib
"""
from PySide import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class HistogramQWidget(QtGui.QWidget):
"""
QT wrapper for matplotlib
"""
def __init__(self, parent=None):
"""
setup GUI
"""
self.data = None
self.value = 0
QtGui.QWidget.__init__(self, parent)
self.my_layout = QtGui.QGridLayout(self)
#self.my_layout.setMargin(0)
self.my_layout.setSpacing(10)
# set up matplotlib
self.dpi = 100
self.fig = Figure((6.0, 6.0), dpi=self.dpi)
self.canvas = FigureCanvas(self.fig)
self.canvas.setMinimumSize(300, 100)
self.canvas.mpl_connect('button_press_event', self.on_click)
#self.canvas.setParent(self)
self.my_layout.addWidget(self.canvas, 0, 0, 1, 2)
self.axes = self.fig.add_subplot(111)
self.cax = None
# add spin box
self.spin_label = QtGui.QLabel(self)
self.spin_label.setText('Value:')
self.my_layout.addWidget(self.spin_label, 1, 0)
self.spin = QtGui.QDoubleSpinBox(self)
self.spin.setMinimum(0.0)
self.spin.setMaximum(1000.0)
#self.spin.setFocusPolicy(QtCore.Qt.NoFocus)
self.spin.setSingleStep(10)
self.spin.setKeyboardTracking(False)
self.spin.setReadOnly(False)
self.my_layout.addWidget(self.spin, 1, 1)
# connect signals
self.connect(self.spin, QtCore.SIGNAL('valueChanged(double)'), self.update_value)
def on_click(self, event):
"""
process click from matplotlib
"""
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(event.button, event.x, event.y, event.xdata, event.ydata)
self.update_value(float(event.xdata))
def update_value(self, v):
"""
update plot
"""
self.value = v
self.spin.setValue(self.value)
self.update_plot()
self.emit(QtCore.SIGNAL('thresholdChanged(double)'), self.value)
def update_dataset(self, dataset):
"""
wrapper for update value
"""
#TODO this can go in subclass
self.value = dataset.threshold
self.data = dataset.red_channel.flatten()
self.update_plot()
self.spin.blockSignals(True)
self.spin.setValue(self.value)
self.spin.blockSignals(False)
def update_plot(self):
"""
plotting routine
"""
print "updating histogram plot"
self.axes.clear()
self.axes.hist(self.data, bins=100, range=(min(self.data), max(self.data)))
self.axes.axvline(x=self.value, linewidth=1, color='r')
self.canvas.draw()