forked from mpmdean/RIXS-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.py
257 lines (204 loc) · 9.64 KB
/
plotter.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import wx, os, glob
import pdb
import numpy as np
import matplotlib
# comment out the following to use wx rather than wxagg
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.patches import Rectangle
# Add display box showing header of file?
#correct zoom using rubbish/rectange_select.py code
#self.axes.remove_patch(self.rect)
#somehow remove patch?
class ControlFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='File Browser', size=(400, 500), pos=(50, 50))
panel = wx.Panel(self, -1)
vbox = wx.BoxSizer(wx.VERTICAL)
bdr_sz = 10 # was 20
# path TextCtrl
vbox.AddSpacer(bdr_sz)
path_cap = wx.StaticText( panel, label='Search Term' )
vbox.Add(path_cap, 0, wx.LEFT | wx.RIGHT | wx.ALIGN_LEFT, border=bdr_sz)
self.path = wx.TextCtrl(panel, size=(140, 20), value =
'/Users/mdean/Dropbox/ALS_feb_2015/La2NiO4_728/*.txt',
style=wx.TE_PROCESS_ENTER)
self.path.Bind(wx.EVT_TEXT_ENTER, self.updatepath)
vbox.Add(self.path, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, border=bdr_sz)
vbox.AddSpacer(bdr_sz)
# LIST BOX SHOWING FILES
self.path_list, self.file_list = self.get_files(self.path.GetValue())
listbox_cap = wx.StaticText( panel, label='Directory contents' )
vbox.Add(listbox_cap, 0, wx.LEFT | wx.RIGHT | wx.ALIGN_LEFT, border=bdr_sz)
self.listbox = wx.ListBox(panel, -1, wx.DefaultPosition, (170, 130), self.file_list, wx.LB_MULTIPLE)
# buttons
vbox.Add(self.listbox, 1, wx.EXPAND | wx.LEFT |wx.RIGHT | wx.BOTTOM, border=bdr_sz)
buttonsize=(80, 20)
hbox = wx.BoxSizer(wx.HORIZONTAL)
self.plot_but = wx.Button(panel, -1, 'Plot', size=buttonsize)
self.Bind(wx.EVT_BUTTON, self.plotit, self.plot_but)
hbox.Add(self.plot_but, 0, wx.ALIGN_LEFT, border=bdr_sz)
hbox.AddSpacer(bdr_sz)
self.clear_but = wx.Button(panel, -1, 'Clear', size=buttonsize)
self.Bind(wx.EVT_BUTTON, self.clearit, self.clear_but)
hbox.Add(self.clear_but, 0, wx.ALIGN_LEFT, border=bdr_sz)
hbox.AddSpacer(bdr_sz)
self.autoscale_but = wx.Button(panel, -1, 'Autoscale', size=buttonsize)
self.Bind(wx.EVT_BUTTON, self.autoscaleit, self.autoscale_but)
hbox.Add(self.autoscale_but, 0, wx.ALIGN_LEFT, border=bdr_sz)
hbox.AddSpacer(bdr_sz)
self.legend_but = wx.Button(panel, -1, 'Legend', size=buttonsize)
self.Bind(wx.EVT_BUTTON, self.legendtoggleit, self.legend_but)
hbox.Add(self.legend_but, 0, wx.ALIGN_LEFT, border=bdr_sz)
vbox.Add(hbox, 0, wx.LEFT | wx.RIGHT | wx.ALIGN_LEFT, border=bdr_sz)
vbox.AddSpacer(bdr_sz)
self.legend_exists = False
# Initiate everything
panel.SetSizer(vbox)
#self.Centre()
self.Show(True)
# Call plotframe
self.plotframe = PlotFrame(self)
self.plotframe.Show()
def updatepath(self, evt):
print "update path"
self.path_list, self.file_list = self.get_files(self.path.GetValue())
self.listbox.Clear()
#self.listbox.InsertItems(self.file_list, 0)
for filelist in self.file_list:
self.listbox.Append(filelist)
#pdb.set_trace()
def plotit(self, evt):
for index in self.listbox.GetSelections():
M = np.loadtxt(self.path_list[index])
filename = os.path.split(self.path_list[index])[1]
self.plotframe.figure.axes[0].plot(M[:,0],M[:,1], label=filename)
self.plotframe.figure.canvas.draw()
def clearit(self, evt):
print "Clear button pressed"
self.plotframe.figure.axes[0].cla()
self.plotframe.figure.canvas.draw()
def autoscaleit(self, evt):
print "Autoscale pressed"
self.plotframe.figure.axes[0].autoscale(True)
self.plotframe.figure.canvas.draw()
def legendtoggleit(self, evt):
print "Legend pressed"
self.legendobj = self.plotframe.axes.legend(prop={'size':9})
if self.legend_exists:
self.legendobj.set_visible(False)
self.legend_exists = False
else:
self.legendobj.set_visible(True)
self.legend_exists = True
self.plotframe.figure.canvas.draw()
# self.legendobj = self.plotframe.axes.legend()
# try:
# self.legendobj.set_visible(False)
# except AttributeError:
# self.legendobj = self.plotframe.axes.legend()
def onselect(vmin, vmax):
print "onselect ran"
print vmin, vmax
def get_files(self, searchterm):
path_list = glob.glob(searchterm)
file_list = []
for path in path_list:
file_list.append(os.path.split(path)[1])
return path_list, file_list
class PlotFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, None, size=(300,500), pos=(500, 50), title='Plot Frame')
self.parent = parent
# initialize plot
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
# Connect the mouse events to their relevant callbacks
self.canvas.mpl_connect('button_press_event', self._onPress)
self.canvas.mpl_connect('button_release_event', self._onRelease)
self.canvas.mpl_connect('motion_notify_event', self._onMotion)
self.pressed = False
# Initialise the rectangle
self.rect = Rectangle((0,0), 1, 1, facecolor='None', visible=False,
edgecolor='k', linestyle='dashed')
self.x0 = 0#None
self.y0 = 0#None
self.x1 = 0#None
self.y1 = 0#None
self.axes.add_patch(self.rect)
self.Fit()
def _onPress(self, event):
''' Callback to handle the mouse being clicked and held over the canvas'''
print "onPress"
# Check the mouse press was actually on the canvas
if event.xdata is not None and event.ydata is not None:
print "In press event data"
# Upon initial press of the mouse record the origin and record the mouse as pressed
self.pressed = True
self.rect.set_visible = True
self.x0 = event.xdata
self.y0 = event.ydata
# Set the width and height and draw the rectangle
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
#self.axes.add_patch(self.rect)
self.canvas.draw()
def _onRelease(self, event):
'''Callback to handle the mouse being released over the canvas'''
print "onRelease"
# Check that the mouse was actually pressed on the canvas to begin with and this isn't a rouge mouse
# release event that started somewhere else
if self.pressed:
# Upon release draw the rectangle as a solid rectangle
self.pressed = False
# Check the mouse was released on the canvas, and if it wasn't then just leave the width and
# height as the last values set by the motion event
if event.xdata is not None and event.ydata is not None:
self.x1 = event.xdata
self.y1 = event.ydata
# Set the width and height and origin of the bounding rectangle
self.boundingRectWidth = self.x1 - self.x0
self.boundingRectHeight = self.y1 - self.y0
self.bouningRectOrigin = (self.x0, self.y0)
# Draw the bounding rectangle
self.rect.set_width(self.boundingRectWidth)
self.rect.set_height(self.boundingRectHeight)
self.rect.set_xy((self.x0, self.y0))
# set zoom
xmin = min([self.x0, self.x1])
xmax = max([self.x0, self.x1])
ymin = min([self.y0, self.y1])
ymax = max([self.y0, self.y1])
self.figure.axes[0].axis([xmin, xmax, ymin, ymax])
self.rect.set_visible = False
self.canvas.draw()
def _onMotion(self, event):
'''Callback to handle the motion event created by the mouse moving over the canvas'''
#print "onMotion"
# If the mouse has been pressed draw an updated rectangle when the mouse is moved so
# the user can see what the current selection is
if self.pressed:
# Check the mouse was released on the canvas, and if it wasn't then just leave the width and
# height as the last values set by the motion event
if event.xdata is not None and event.ydata is not None:
self.x1 = event.xdata
self.y1 = event.ydata
# Set the width and height and draw the rectangle
self.rect.set_visible = True
self.rect.set_width(self.x1 - self.x0)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((self.x0, self.y0))
#self.rectpatch = self.axes.add_patch(self.rect)
self.canvas.draw()
def OnPaint(self, event):
self.canvas.draw()
if __name__ == "__main__":
App=wx.PySimpleApp()
ControlFrame().Show()
App.MainLoop()