1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import sys |
---|
5 | import matplotlib |
---|
6 | matplotlib.use('Qt4Agg') |
---|
7 | matplotlib.rcParams['backend.qt4']='PySide' |
---|
8 | import pylab |
---|
9 | |
---|
10 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
11 | from matplotlib.figure import Figure |
---|
12 | |
---|
13 | from PySide import QtCore, QtGui |
---|
14 | |
---|
15 | from MatplotlibWidget import MatplotlibWidget |
---|
16 | import epics |
---|
17 | import datetime |
---|
18 | import time |
---|
19 | |
---|
20 | |
---|
21 | TIME_PV = 'S:SRtimeCP' |
---|
22 | CURRENT_PV = 'S:SRcurrentCP' |
---|
23 | |
---|
24 | |
---|
25 | def _readAsciiData(): |
---|
26 | ''''read test data from text file''' |
---|
27 | with open('aps_data.txt', 'r') as fp: |
---|
28 | buf = fp.read() |
---|
29 | x, y = [], [] |
---|
30 | for line in buf.splitlines(): |
---|
31 | line = line.strip() |
---|
32 | if len(line) > 0 and not line.startswith('#'): # skip any comment lines |
---|
33 | try: |
---|
34 | fx, fy = map(float, line.split()) |
---|
35 | x.append(fx) |
---|
36 | y.append(fy) |
---|
37 | except: |
---|
38 | pass |
---|
39 | timestamp = '2013-07-09 15:00:00' # approximate |
---|
40 | epoch = time.mktime(time.strptime(timestamp, '%Y-%m-%d %H:%M:%S')) |
---|
41 | return epoch, x, y |
---|
42 | |
---|
43 | |
---|
44 | def _readXmlData(): |
---|
45 | ''''read test data from XML file''' |
---|
46 | import xml.etree.cElementTree as ElementTree |
---|
47 | root = ElementTree.parse('aps_data.xml').getroot() |
---|
48 | timestamp = root.attrib['datetime'] |
---|
49 | epoch = time.mktime(time.strptime(timestamp, '%Y-%m-%d %H:%M:%S')) |
---|
50 | x = map(float, root.find('time').text.strip().split()) |
---|
51 | y = map(float, root.find('current').text.strip().split()) |
---|
52 | return epoch, x, y |
---|
53 | |
---|
54 | |
---|
55 | def getTestData(): |
---|
56 | '''get some test data, either from EPICS or a local data file''' |
---|
57 | xpv = epics.PV(TIME_PV) |
---|
58 | ypv = epics.PV(CURRENT_PV) |
---|
59 | if xpv.connected and ypv.connected: |
---|
60 | x, y = xpv.value, ypv.value |
---|
61 | epoch = time.mktime(time.gmtime()) |
---|
62 | else: |
---|
63 | #epoch, x, y = _readAsciiData() |
---|
64 | epoch, x, y = _readXmlData() |
---|
65 | return epoch, x, y |
---|
66 | |
---|
67 | |
---|
68 | class MplWidget(QtGui.QWidget): |
---|
69 | |
---|
70 | def __init__(self): |
---|
71 | QtGui.QWidget.__init__(self) |
---|
72 | layout = QtGui.QVBoxLayout() |
---|
73 | self.setLayout(layout) |
---|
74 | |
---|
75 | widget = MatplotlibWidget(title='APS Storage Ring 24-hour History', |
---|
76 | xlabel='time before now, ht', |
---|
77 | ylabel='', |
---|
78 | hold=True, |
---|
79 | showgrid=True, |
---|
80 | #xticks=[-24,-18,-6,0], |
---|
81 | yticks=[0,25,50,75,100], |
---|
82 | ) |
---|
83 | layout.addWidget(widget) |
---|
84 | |
---|
85 | epoch, time_data, current_data = getTestData() |
---|
86 | widget.addPlot(time_data, current_data, 'r-', label='history') |
---|
87 | widget.addPlot([-5, -2], [1,20], 'bo-', label='2 points') |
---|
88 | widget.axes.legend() |
---|
89 | ''' |
---|
90 | MatPlotLib mouse events |
---|
91 | |
---|
92 | http://matplotlib.org/users/event_handling.html |
---|
93 | |
---|
94 | example handling: |
---|
95 | |
---|
96 | >>> print event |
---|
97 | MPL MouseEvent: xy=(492,105) xydata=(-4.23387096774,25.71875) button=1 dblclick=False inaxes=Axes(0.125,0.1;0.775x0.363636) |
---|
98 | ''' |
---|
99 | self.default_color = dict(figure=None, axes=None) |
---|
100 | widget.mpl_connect('button_press_event', self.on_button_press) |
---|
101 | widget.mpl_connect('button_release_event', self.on_button_release) |
---|
102 | widget.mpl_connect('figure_enter_event', self.on_figure_enter) |
---|
103 | widget.mpl_connect('figure_leave_event', self.on_figure_leave) |
---|
104 | widget.mpl_connect('axes_enter_event', self.on_axes_enter) |
---|
105 | widget.mpl_connect('axes_leave_event', self.on_axes_leave) |
---|
106 | |
---|
107 | # changing colors and print text to demo the handlers |
---|
108 | # TODO: replace demo code with real functions such as a proper zoom stack |
---|
109 | def on_button_press(self, event): |
---|
110 | print 'on_button_press: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % ( |
---|
111 | event.button, event.x, event.y, event.xdata, event.ydata) |
---|
112 | |
---|
113 | def on_button_release(self, event): |
---|
114 | print 'on_button_release: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % ( |
---|
115 | event.button, event.x, event.y, event.xdata, event.ydata) |
---|
116 | |
---|
117 | def on_figure_enter(self, event): |
---|
118 | fig = event.canvas.figure |
---|
119 | print 'on_figure_enter: %s' % str(fig) |
---|
120 | if self.default_color['figure'] is None: |
---|
121 | self.default_color['figure'] = fig.get_facecolor() |
---|
122 | fig.set_facecolor('bisque') |
---|
123 | event.canvas.draw() |
---|
124 | |
---|
125 | def on_figure_leave(self, event): |
---|
126 | fig = event.canvas.figure |
---|
127 | print 'on_figure_leave: %s' % str(fig) |
---|
128 | if self.default_color['figure'] is not None: |
---|
129 | fig.set_facecolor(self.default_color['figure']) |
---|
130 | event.canvas.draw() |
---|
131 | |
---|
132 | def on_axes_enter(self, event): |
---|
133 | axes = event.inaxes |
---|
134 | print 'on_axes_enter: %s, x=%d, y=%d, xdata=%f, ydata=%f' % ( |
---|
135 | str(axes), event.x, event.y, event.xdata, event.ydata) |
---|
136 | if self.default_color['axes'] is None: |
---|
137 | self.default_color['axes'] = axes.patch.get_facecolor() |
---|
138 | axes.patch.set_facecolor('#ccccff') |
---|
139 | event.canvas.draw() |
---|
140 | |
---|
141 | def on_axes_leave(self, event): |
---|
142 | axes = event.inaxes |
---|
143 | print 'on_axes_leave: %s, x=%d, y=%d, xdata=%f, ydata=%f' % ( |
---|
144 | str(axes), event.x, event.y, event.xdata, event.ydata) |
---|
145 | if self.default_color['axes'] is not None: |
---|
146 | axes.patch.set_facecolor(self.default_color['axes']) |
---|
147 | event.canvas.draw() |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | class PlotMyWay(QtGui.QWidget): |
---|
152 | |
---|
153 | def __init__(self): |
---|
154 | QtGui.QWidget.__init__(self) |
---|
155 | layout = QtGui.QVBoxLayout() |
---|
156 | self.setLayout(layout) |
---|
157 | |
---|
158 | fig = Figure() |
---|
159 | fig.suptitle('matplotlib testing') |
---|
160 | xpv = epics.PV(TIME_PV) |
---|
161 | ypv = epics.PV(CURRENT_PV) |
---|
162 | epoch, x, y = getTestData() |
---|
163 | ax = fig.add_subplot(111) |
---|
164 | ax.set_xlabel('time since now, hours') |
---|
165 | ax.set_ylabel('APS storage ring current, mA') |
---|
166 | ax.set_title('APS Storage Ring 24-hour History') |
---|
167 | ax.grid(True) |
---|
168 | ax.plot(x, y, 'r-') |
---|
169 | layout.addWidget( FigureCanvas(fig) ) |
---|
170 | fig.canvas.mpl_connect('button_release_event', self.on_button_release) |
---|
171 | |
---|
172 | def on_button_release(self, event): |
---|
173 | '''MatPlotLib mouse event |
---|
174 | |
---|
175 | http://matplotlib.org/users/event_handling.html |
---|
176 | |
---|
177 | MPL MouseEvent: xy=(492,105) xydata=(-4.23387096774,25.71875) button=1 dblclick=False inaxes=Axes(0.125,0.1;0.775x0.363636) |
---|
178 | ''' |
---|
179 | print event |
---|
180 | |
---|
181 | |
---|
182 | def makeFigure(): |
---|
183 | # generate the plot |
---|
184 | fig = Figure(figsize=(600,600), dpi=72, facecolor=(1,1,1), edgecolor=(0,0,0)) |
---|
185 | ax = fig.add_subplot(111) |
---|
186 | ax.plot([0,1]) |
---|
187 | return fig |
---|
188 | |
---|
189 | |
---|
190 | def main(): |
---|
191 | app = QtGui.QApplication(sys.argv) |
---|
192 | canvas = FigureCanvas(makeFigure()) # generate the canvas to display the plot |
---|
193 | win = QtGui.QMainWindow() |
---|
194 | win.setCentralWidget(canvas) # add the plot canvas to a window |
---|
195 | win.show() |
---|
196 | sys.exit(app.exec_()) |
---|
197 | |
---|
198 | |
---|
199 | def myway(): |
---|
200 | app = QtGui.QApplication(sys.argv) |
---|
201 | view = PlotMyWay() |
---|
202 | view.show() |
---|
203 | sys.exit(app.exec_()) |
---|
204 | |
---|
205 | |
---|
206 | def mplw(): |
---|
207 | app = QtGui.QApplication(sys.argv) |
---|
208 | view = MplWidget() |
---|
209 | view.show() |
---|
210 | sys.exit(app.exec_()) |
---|
211 | |
---|
212 | |
---|
213 | if __name__ == '__main__': |
---|
214 | #main() |
---|
215 | #myway() |
---|
216 | mplw() |
---|