1 | import wx.lib.newevent |
---|
2 | import matplotlib |
---|
3 | matplotlib.interactive(False) |
---|
4 | #Use the WxAgg back end. The Wx one takes too long to render |
---|
5 | matplotlib.use('WXAgg') |
---|
6 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg |
---|
7 | from matplotlib.figure import Figure |
---|
8 | import os |
---|
9 | import fittings |
---|
10 | import transform |
---|
11 | from canvas import FigureCanvas |
---|
12 | from matplotlib.widgets import RectangleSelector |
---|
13 | from pylab import gca, gcf |
---|
14 | from plottables import Theory1D |
---|
15 | #TODO: make the plottables interactive |
---|
16 | from binder import BindArtist |
---|
17 | |
---|
18 | DEBUG = False |
---|
19 | |
---|
20 | from plottables import Graph |
---|
21 | #(FuncFitEvent, EVT_FUNC_FIT) = wx.lib.newevent.NewEvent() |
---|
22 | import math,pylab,re |
---|
23 | |
---|
24 | def show_tree(obj,d=0): |
---|
25 | """Handy function for displaying a tree of graph objects""" |
---|
26 | print "%s%s" % ("-"*d,obj.__class__.__name__) |
---|
27 | if 'get_children' in dir(obj): |
---|
28 | for a in obj.get_children(): show_tree(a,d+1) |
---|
29 | |
---|
30 | from unitConverter import UnitConvertion as convertUnit |
---|
31 | def _convertUnit(pow,unit): |
---|
32 | """ |
---|
33 | Displays the unit with the proper convertion |
---|
34 | @param pow: the user set the power of the unit |
---|
35 | @param unit: the unit of the data |
---|
36 | """ |
---|
37 | return unit |
---|
38 | toks=re.match("^", unit) |
---|
39 | if not toks==None: |
---|
40 | unitValue= re.split("{",unit) |
---|
41 | unitPower= re.split("}",unitValue[1]) |
---|
42 | power= int(unitPower[0])*pow |
---|
43 | word= unitValue[0]+"{"+str(power)+"}" |
---|
44 | if power==1: |
---|
45 | tempUnit=re.split("\^",unitValue[0]) |
---|
46 | unit=tempUnit[0] |
---|
47 | else: |
---|
48 | unit = word |
---|
49 | #print"this is unit",unit |
---|
50 | return unit |
---|
51 | def _rescale(lo,hi,step,pt=None,bal=None,scale='linear'): |
---|
52 | """ |
---|
53 | Rescale (lo,hi) by step, returning the new (lo,hi) |
---|
54 | The scaling is centered on pt, with positive values of step |
---|
55 | driving lo/hi away from pt and negative values pulling them in. |
---|
56 | If bal is given instead of point, it is already in [0,1] coordinates. |
---|
57 | |
---|
58 | This is a helper function for step-based zooming. |
---|
59 | """ |
---|
60 | # Convert values into the correct scale for a linear transformation |
---|
61 | # TODO: use proper scale transformers |
---|
62 | loprev = lo |
---|
63 | hiprev = hi |
---|
64 | ptprev = pt |
---|
65 | if scale=='log': |
---|
66 | assert lo >0 |
---|
67 | if lo > 0 : |
---|
68 | lo = math.log10(lo) |
---|
69 | if hi > 0 : |
---|
70 | hi = math.log10(hi) |
---|
71 | if pt is not None: pt = math.log10(pt) |
---|
72 | |
---|
73 | # Compute delta from axis range * %, or 1-% if persent is negative |
---|
74 | if step > 0: |
---|
75 | delta = float(hi-lo)*step/100 |
---|
76 | else: |
---|
77 | delta = float(hi-lo)*step/(100-step) |
---|
78 | |
---|
79 | # Add scale factor proportionally to the lo and hi values, preserving the |
---|
80 | # point under the mouse |
---|
81 | if bal is None: |
---|
82 | bal = float(pt-lo)/(hi-lo) |
---|
83 | lo = lo - bal*delta |
---|
84 | hi = hi + (1-bal)*delta |
---|
85 | |
---|
86 | # Convert transformed values back to the original scale |
---|
87 | if scale=='log': |
---|
88 | if (lo <= -250) or (hi >= 250): |
---|
89 | lo=loprev |
---|
90 | hi=hiprev |
---|
91 | else: |
---|
92 | lo,hi = math.pow(10.,lo),math.pow(10.,hi) |
---|
93 | return (lo,hi) |
---|
94 | |
---|
95 | |
---|
96 | class PlotPanel(wx.Panel): |
---|
97 | """ |
---|
98 | The PlotPanel has a Figure and a Canvas. OnSize events simply set a |
---|
99 | flag, and the actually redrawing of the |
---|
100 | figure is triggered by an Idle event. |
---|
101 | """ |
---|
102 | def __init__(self, parent, id = -1, color = None,\ |
---|
103 | dpi = None, **kwargs): |
---|
104 | wx.Panel.__init__(self, parent, id = id, **kwargs) |
---|
105 | self.parent = parent |
---|
106 | self.figure = Figure(None, dpi) |
---|
107 | #self.figure = pylab.Figure(None, dpi) |
---|
108 | #self.canvas = NoRepaintCanvas(self, -1, self.figure) |
---|
109 | self.canvas = FigureCanvas(self, -1, self.figure) |
---|
110 | self.SetColor(color) |
---|
111 | #self.Bind(wx.EVT_IDLE, self._onIdle) |
---|
112 | #self.Bind(wx.EVT_SIZE, self._onSize) |
---|
113 | self._resizeflag = True |
---|
114 | self._SetSize() |
---|
115 | self.subplot = self.figure.add_subplot(111) |
---|
116 | self.figure.subplots_adjust(left=.2, bottom=.2) |
---|
117 | self.yscale = 'linear' |
---|
118 | self.xscale = 'linear' |
---|
119 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
120 | sizer.Add(self.canvas,1,wx.EXPAND) |
---|
121 | self.SetSizer(sizer) |
---|
122 | |
---|
123 | # Graph object to manage the plottables |
---|
124 | self.graph = Graph() |
---|
125 | #self.Bind(EVT_FUNC_FIT, self.onFitRange) |
---|
126 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
127 | |
---|
128 | #self.Bind(EVT_PROPERTY, self._onEVT_FUNC_PROPERTY) |
---|
129 | # Define some constants |
---|
130 | self.colorlist = ['b','g','r','c','m','y'] |
---|
131 | self.symbollist = ['o','x','^','v','<','>','+','s','d','D','h','H','p'] |
---|
132 | #User scale |
---|
133 | self.xLabel ="x" |
---|
134 | self.yLabel ="y" |
---|
135 | self.viewModel ="--" |
---|
136 | # keep track if the previous transformation of x and y in Property dialog |
---|
137 | self.prevXtrans ="x" |
---|
138 | self.prevYtrans ="y" |
---|
139 | self.canvas.mpl_connect('scroll_event',self.onWheel) |
---|
140 | #taking care of dragging |
---|
141 | self.canvas.mpl_connect('motion_notify_event',self.onMouseMotion) |
---|
142 | self.canvas.mpl_connect('button_press_event',self.onLeftDown) |
---|
143 | self.canvas.mpl_connect('button_release_event',self.onLeftUp) |
---|
144 | |
---|
145 | |
---|
146 | self.leftdown=False |
---|
147 | self.leftup=False |
---|
148 | self.mousemotion=False |
---|
149 | |
---|
150 | self.axes = [self.subplot] |
---|
151 | |
---|
152 | # Interactor |
---|
153 | self.connect = BindArtist(self.subplot.figure) |
---|
154 | #self.selected_plottable = None |
---|
155 | |
---|
156 | # new data for the fit |
---|
157 | self.fit_result = Theory1D(x=[], y=[], dy=None) |
---|
158 | #self.fit_result = Data1D(x=[], y=[],dx=None, dy=None) |
---|
159 | self.fit_result.name = "Fit" |
---|
160 | # For fit Dialog initial display |
---|
161 | self.xmin=0.0 |
---|
162 | self.xmax=0.0 |
---|
163 | self.xminView=0.0 |
---|
164 | self.xmaxView=0.0 |
---|
165 | self.Avalue=None |
---|
166 | self.Bvalue=None |
---|
167 | self.ErrAvalue=None |
---|
168 | self.ErrBvalue=None |
---|
169 | self.Chivalue=None |
---|
170 | |
---|
171 | # Dragging info |
---|
172 | self.begDrag=False |
---|
173 | self.xInit=None |
---|
174 | self.yInit=None |
---|
175 | self.xFinal=None |
---|
176 | self.yFinal=None |
---|
177 | |
---|
178 | # Default locations |
---|
179 | self._default_save_location = os.getcwd() |
---|
180 | |
---|
181 | |
---|
182 | def onLeftDown(self,event): |
---|
183 | """ left button down and ready to drag""" |
---|
184 | self.leftdown=True |
---|
185 | ax = event.inaxes |
---|
186 | if ax != None: |
---|
187 | self.xInit,self.yInit=event.xdata,event.ydata |
---|
188 | |
---|
189 | |
---|
190 | def onLeftUp(self,event): |
---|
191 | """ Dragging is done """ |
---|
192 | self.leftdown=False |
---|
193 | self.mousemotion=False |
---|
194 | self.leftup=True |
---|
195 | |
---|
196 | def onMouseMotion(self,event): |
---|
197 | """ |
---|
198 | check if the left button is press and the mouse in moving. |
---|
199 | computer delta for x and y coordinates and then calls draghelper |
---|
200 | to perform the drag |
---|
201 | """ |
---|
202 | self.mousemotion=True |
---|
203 | if self.leftdown==True and self.mousemotion==True: |
---|
204 | |
---|
205 | ax = event.inaxes |
---|
206 | if ax !=None:#the dragging is perform inside the figure |
---|
207 | self.xFinal,self.yFinal=event.xdata,event.ydata |
---|
208 | |
---|
209 | # Check whether this is the first point |
---|
210 | if self.xInit==None: |
---|
211 | self.xInit = self.xFinal |
---|
212 | self.yInit = self.yFinal |
---|
213 | |
---|
214 | xdelta = self.xFinal -self.xInit |
---|
215 | ydelta = self.yFinal -self.yInit |
---|
216 | |
---|
217 | if self.xscale=='log': |
---|
218 | xdelta = math.log10(self.xFinal) -math.log10(self.xInit) |
---|
219 | if self.yscale=='log': |
---|
220 | ydelta = math.log10(self.yFinal) -math.log10(self.yInit) |
---|
221 | self.dragHelper(xdelta,ydelta) |
---|
222 | |
---|
223 | else:# no dragging is perform elsewhere |
---|
224 | self.dragHelper(0,0) |
---|
225 | |
---|
226 | def dragHelper(self,xdelta,ydelta): |
---|
227 | """ dragging occurs here""" |
---|
228 | |
---|
229 | # Event occurred inside a plotting area |
---|
230 | for ax in self.axes: |
---|
231 | lo,hi= ax.get_xlim() |
---|
232 | #print "x lo %f and x hi %f"%(lo,hi) |
---|
233 | newlo,newhi= lo- xdelta, hi- xdelta |
---|
234 | if self.xscale=='log': |
---|
235 | if lo > 0: |
---|
236 | newlo= math.log10(lo)-xdelta |
---|
237 | if hi > 0: |
---|
238 | newhi= math.log10(hi)-xdelta |
---|
239 | if self.xscale=='log': |
---|
240 | ax.set_xlim(math.pow(10,newlo),math.pow(10,newhi)) |
---|
241 | else: |
---|
242 | ax.set_xlim(newlo,newhi) |
---|
243 | #print "new lo %f and new hi %f"%(newlo,newhi) |
---|
244 | |
---|
245 | lo,hi= ax.get_ylim() |
---|
246 | #print "y lo %f and y hi %f"%(lo,hi) |
---|
247 | newlo,newhi= lo- ydelta, hi- ydelta |
---|
248 | if self.yscale=='log': |
---|
249 | if lo > 0: |
---|
250 | newlo= math.log10(lo)-ydelta |
---|
251 | if hi > 0: |
---|
252 | newhi= math.log10(hi)-ydelta |
---|
253 | #print "new lo %f and new hi %f"%(newlo,newhi) |
---|
254 | if self.yscale=='log': |
---|
255 | ax.set_ylim(math.pow(10,newlo),math.pow(10,newhi)) |
---|
256 | else: |
---|
257 | ax.set_ylim(newlo,newhi) |
---|
258 | self.canvas.draw_idle() |
---|
259 | |
---|
260 | |
---|
261 | |
---|
262 | def resetFitView(self): |
---|
263 | """ |
---|
264 | For fit Dialog initial display |
---|
265 | """ |
---|
266 | self.xmin=0.0 |
---|
267 | self.xmax=0.0 |
---|
268 | self.xminView=0.0 |
---|
269 | self.xmaxView=0.0 |
---|
270 | self.Avalue=None |
---|
271 | self.Bvalue=None |
---|
272 | self.ErrAvalue=None |
---|
273 | self.ErrBvalue=None |
---|
274 | self.Chivalue=None |
---|
275 | |
---|
276 | def onWheel(self, event): |
---|
277 | """ |
---|
278 | Process mouse wheel as zoom events |
---|
279 | @param event: Wheel event |
---|
280 | """ |
---|
281 | ax = event.inaxes |
---|
282 | step = event.step |
---|
283 | |
---|
284 | if ax != None: |
---|
285 | # Event occurred inside a plotting area |
---|
286 | lo,hi = ax.get_xlim() |
---|
287 | lo,hi = _rescale(lo,hi,step,pt=event.xdata,scale=ax.get_xscale()) |
---|
288 | if not self.xscale=='log' or lo>0: |
---|
289 | ax.set_xlim((lo,hi)) |
---|
290 | |
---|
291 | lo,hi = ax.get_ylim() |
---|
292 | lo,hi = _rescale(lo,hi,step,pt=event.ydata,scale=ax.get_yscale()) |
---|
293 | if not self.yscale=='log' or lo>0: |
---|
294 | ax.set_ylim((lo,hi)) |
---|
295 | else: |
---|
296 | # Check if zoom happens in the axes |
---|
297 | xdata,ydata = None,None |
---|
298 | x,y = event.x,event.y |
---|
299 | |
---|
300 | for ax in self.axes: |
---|
301 | insidex,_ = ax.xaxis.contains(event) |
---|
302 | if insidex: |
---|
303 | xdata,_ = ax.transAxes.inverse_xy_tup((x,y)) |
---|
304 | insidey,_ = ax.yaxis.contains(event) |
---|
305 | if insidey: |
---|
306 | _,ydata = ax.transAxes.inverse_xy_tup((x,y)) |
---|
307 | if xdata is not None: |
---|
308 | lo,hi = ax.get_xlim() |
---|
309 | lo,hi = _rescale(lo,hi,step,bal=xdata,scale=ax.get_xscale()) |
---|
310 | if not self.xscale=='log' or lo>0: |
---|
311 | ax.set_xlim((lo,hi)) |
---|
312 | if ydata is not None: |
---|
313 | lo,hi = ax.get_ylim() |
---|
314 | lo,hi = _rescale(lo,hi,step,bal=ydata,scale=ax.get_yscale()) |
---|
315 | if not self.yscale=='log' or lo>0: |
---|
316 | ax.set_ylim((lo,hi)) |
---|
317 | |
---|
318 | self.canvas.draw_idle() |
---|
319 | |
---|
320 | |
---|
321 | def returnTrans(self): |
---|
322 | """ |
---|
323 | Return values and labels used by Fit Dialog |
---|
324 | """ |
---|
325 | return self.xLabel,self.yLabel, self.Avalue, self.Bvalue,\ |
---|
326 | self.ErrAvalue,self.ErrBvalue,self.Chivalue |
---|
327 | |
---|
328 | def setTrans(self,xtrans,ytrans): |
---|
329 | """ |
---|
330 | @param xtrans: set x transformation on Property dialog |
---|
331 | @param ytrans: set y transformation on Property dialog |
---|
332 | """ |
---|
333 | self.prevXtrans =xtrans |
---|
334 | self.prevYtrans =ytrans |
---|
335 | |
---|
336 | def onFitting(self, event): |
---|
337 | """ |
---|
338 | when clicking on linear Fit on context menu , display Fitting Dialog |
---|
339 | """ |
---|
340 | list =[] |
---|
341 | list = self.graph.returnPlottable() |
---|
342 | from fitDialog import LinearFit |
---|
343 | |
---|
344 | if len(list.keys())>0: |
---|
345 | first_item = list.keys()[0] |
---|
346 | dlg = LinearFit( None, first_item, self.onFitDisplay,self.returnTrans, -1, 'Linear Fit') |
---|
347 | |
---|
348 | if (self.xmin !=0.0 )and ( self.xmax !=0.0)\ |
---|
349 | and(self.xminView !=0.0 )and ( self.xmaxView !=0.0): |
---|
350 | dlg.setFitRange(self.xminView,self.xmaxView,self.xmin,self.xmax) |
---|
351 | dlg.ShowModal() |
---|
352 | |
---|
353 | def linear_plottable_fit(self, plot): |
---|
354 | """ |
---|
355 | when clicking on linear Fit on context menu , display Fitting Dialog |
---|
356 | """ |
---|
357 | from fitDialog import LinearFit |
---|
358 | |
---|
359 | dlg = LinearFit( None, plot, self.onFitDisplay,self.returnTrans, -1, 'Linear Fit') |
---|
360 | |
---|
361 | if (self.xmin !=0.0 )and ( self.xmax !=0.0)\ |
---|
362 | and(self.xminView !=0.0 )and ( self.xmaxView !=0.0): |
---|
363 | dlg.setFitRange(self.xminView,self.xmaxView,self.xmin,self.xmax) |
---|
364 | dlg.ShowModal() |
---|
365 | |
---|
366 | def _onProperties(self, event): |
---|
367 | """ |
---|
368 | when clicking on Properties on context menu ,The Property dialog is displayed |
---|
369 | The user selects a transformation for x or y value and a new plot is displayed |
---|
370 | """ |
---|
371 | list =[] |
---|
372 | list = self.graph.returnPlottable() |
---|
373 | if len(list.keys())>0: |
---|
374 | first_item = list.keys()[0] |
---|
375 | if first_item.x !=[]: |
---|
376 | from PropertyDialog import Properties |
---|
377 | dial = Properties(self, -1, 'Properties') |
---|
378 | dial.setValues( self.prevXtrans, self.prevYtrans,self.viewModel ) |
---|
379 | if dial.ShowModal() == wx.ID_OK: |
---|
380 | self.xLabel, self.yLabel,self.viewModel = dial.getValues() |
---|
381 | if self.viewModel =="Guinier lny vs x^(2)": |
---|
382 | self.xLabel="x^(2)" |
---|
383 | self.yLabel="ln(y)" |
---|
384 | self.viewModel = "--" |
---|
385 | dial.setValues( self.xLabel, self.yLabel,self.viewModel ) |
---|
386 | self._onEVT_FUNC_PROPERTY() |
---|
387 | dial.Destroy() |
---|
388 | |
---|
389 | |
---|
390 | def set_yscale(self, scale='linear'): |
---|
391 | """ |
---|
392 | Set the scale on Y-axis |
---|
393 | @param scale: the scale of y-axis |
---|
394 | """ |
---|
395 | self.subplot.set_yscale(scale) |
---|
396 | self.yscale = scale |
---|
397 | |
---|
398 | def get_yscale(self): |
---|
399 | """ |
---|
400 | @return: Y-axis scale |
---|
401 | """ |
---|
402 | return self.yscale |
---|
403 | |
---|
404 | def set_xscale(self, scale='linear'): |
---|
405 | """ |
---|
406 | Set the scale on x-axis |
---|
407 | @param scale: the scale of x-axis |
---|
408 | """ |
---|
409 | self.subplot.set_xscale(scale) |
---|
410 | self.xscale = scale |
---|
411 | |
---|
412 | def get_xscale(self): |
---|
413 | """ |
---|
414 | @return: x-axis scale |
---|
415 | """ |
---|
416 | return self.xscale |
---|
417 | |
---|
418 | def SetColor(self, rgbtuple): |
---|
419 | """Set figure and canvas colours to be the same""" |
---|
420 | if not rgbtuple: |
---|
421 | rgbtuple = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE).Get() |
---|
422 | col = [c/255.0 for c in rgbtuple] |
---|
423 | self.figure.set_facecolor(col) |
---|
424 | self.figure.set_edgecolor(col) |
---|
425 | self.canvas.SetBackgroundColour(wx.Colour(*rgbtuple)) |
---|
426 | |
---|
427 | def _onSize(self, event): |
---|
428 | self._resizeflag = True |
---|
429 | |
---|
430 | def _onIdle(self, evt): |
---|
431 | if self._resizeflag: |
---|
432 | self._resizeflag = False |
---|
433 | self._SetSize() |
---|
434 | self.draw() |
---|
435 | |
---|
436 | def _SetSize(self, pixels = None): |
---|
437 | """ |
---|
438 | This method can be called to force the Plot to be a desired size, which defaults to |
---|
439 | the ClientSize of the panel |
---|
440 | """ |
---|
441 | if not pixels: |
---|
442 | pixels = self.GetClientSize() |
---|
443 | self.canvas.SetSize(pixels) |
---|
444 | self.figure.set_size_inches(pixels[0]/self.figure.get_dpi(), |
---|
445 | pixels[1]/self.figure.get_dpi()) |
---|
446 | |
---|
447 | def draw(self): |
---|
448 | """Where the actual drawing happens""" |
---|
449 | self.figure.canvas.draw_idle() |
---|
450 | |
---|
451 | |
---|
452 | def onSaveImage(self, evt): |
---|
453 | #figure.savefig |
---|
454 | #print "Save image not implemented" |
---|
455 | path = None |
---|
456 | dlg = wx.FileDialog(self, "Choose a file", self._default_save_location, "", "*.png", wx.SAVE) |
---|
457 | if dlg.ShowModal() == wx.ID_OK: |
---|
458 | path = dlg.GetPath() |
---|
459 | self._default_save_location = os.path.dirname(path) |
---|
460 | dlg.Destroy() |
---|
461 | if not path == None: |
---|
462 | self.subplot.figure.savefig(path,dpi=300, facecolor='w', edgecolor='w', |
---|
463 | orentation='portrait', papertype=None, format='png') |
---|
464 | |
---|
465 | def onContextMenu(self, event): |
---|
466 | """ |
---|
467 | Default context menu for a plot panel |
---|
468 | """ |
---|
469 | # Slicer plot popup menu |
---|
470 | id = wx.NewId() |
---|
471 | slicerpop = wx.Menu() |
---|
472 | slicerpop.Append(id,'&Save image', 'Save image as PNG') |
---|
473 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
474 | |
---|
475 | #id = wx.NewId() |
---|
476 | #slicerpop.Append(id, '&Load 1D data file') |
---|
477 | #wx.EVT_MENU(self, id, self._onLoad1DData) |
---|
478 | |
---|
479 | id = wx.NewId() |
---|
480 | slicerpop.AppendSeparator() |
---|
481 | slicerpop.Append(id, '&Properties') |
---|
482 | wx.EVT_MENU(self, id, self._onProperties) |
---|
483 | |
---|
484 | id = wx.NewId() |
---|
485 | slicerpop.AppendSeparator() |
---|
486 | slicerpop.Append(id, '&Linear Fit') |
---|
487 | wx.EVT_MENU(self, id, self.onFitting) |
---|
488 | |
---|
489 | id = wx.NewId() |
---|
490 | slicerpop.AppendSeparator() |
---|
491 | slicerpop.Append(id, '&Reset Graph') |
---|
492 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
493 | |
---|
494 | pos = event.GetPosition() |
---|
495 | pos = self.ScreenToClient(pos) |
---|
496 | self.PopupMenu(slicerpop, pos) |
---|
497 | |
---|
498 | ## The following is plottable functionality |
---|
499 | |
---|
500 | |
---|
501 | def properties(self,prop): |
---|
502 | """Set some properties of the graph. |
---|
503 | |
---|
504 | The set of properties is not yet determined. |
---|
505 | """ |
---|
506 | # The particulars of how they are stored and manipulated (e.g., do |
---|
507 | # we want an inventory internally) is not settled. I've used a |
---|
508 | # property dictionary for now. |
---|
509 | # |
---|
510 | # How these properties interact with a user defined style file is |
---|
511 | # even less clear. |
---|
512 | |
---|
513 | # Properties defined by plot |
---|
514 | self.subplot.set_xlabel(r"$%s$" % prop["xlabel"]) |
---|
515 | self.subplot.set_ylabel(r"$%s$" % prop["ylabel"]) |
---|
516 | self.subplot.set_title(prop["title"]) |
---|
517 | |
---|
518 | # Properties defined by user |
---|
519 | #self.axes.grid(True) |
---|
520 | |
---|
521 | def clear(self): |
---|
522 | """Reset the plot""" |
---|
523 | |
---|
524 | # TODO: Redraw is brutal. Render to a backing store and swap in |
---|
525 | # TODO: rather than redrawing on the fly. |
---|
526 | self.subplot.clear() |
---|
527 | self.subplot.hold(True) |
---|
528 | |
---|
529 | def render(self): |
---|
530 | """Commit the plot after all objects are drawn""" |
---|
531 | # TODO: this is when the backing store should be swapped in. |
---|
532 | from matplotlib.font_manager import FontProperties |
---|
533 | self.subplot.legend(prop=FontProperties(size=10)) |
---|
534 | #self.subplot.legend() |
---|
535 | pass |
---|
536 | |
---|
537 | def xaxis(self,label,units): |
---|
538 | """xaxis label and units. |
---|
539 | |
---|
540 | Axis labels know about units. |
---|
541 | |
---|
542 | We need to do this so that we can detect when axes are not |
---|
543 | commesurate. Currently this is ignored other than for formatting |
---|
544 | purposes. |
---|
545 | """ |
---|
546 | if units != "": label = label + " (" + units + ")" |
---|
547 | self.subplot.set_xlabel(label) |
---|
548 | pass |
---|
549 | |
---|
550 | def yaxis(self,label,units): |
---|
551 | """yaxis label and units.""" |
---|
552 | if units != "": label = label + " (" + units + ")" |
---|
553 | self.subplot.set_ylabel(label) |
---|
554 | pass |
---|
555 | |
---|
556 | def _connect_to_xlim(self,callback): |
---|
557 | """Bind the xlim change notification to the callback""" |
---|
558 | def process_xlim(axes): |
---|
559 | lo,hi = subplot.get_xlim() |
---|
560 | callback(lo,hi) |
---|
561 | self.subplot.callbacks.connect('xlim_changed',process_xlim) |
---|
562 | |
---|
563 | #def connect(self,trigger,callback): |
---|
564 | # print "PlotPanel.connect???" |
---|
565 | # if trigger == 'xlim': self._connect_to_xlim(callback) |
---|
566 | |
---|
567 | def interactive_points(self,x,y,dx=None,dy=None,name='', color=0,symbol=0,label=None): |
---|
568 | """Draw markers with error bars""" |
---|
569 | self.subplot.set_yscale('linear') |
---|
570 | self.subplot.set_xscale('linear') |
---|
571 | |
---|
572 | from plottable_interactor import PointInteractor |
---|
573 | p = PointInteractor(self, self.subplot, zorder=3, id=name) |
---|
574 | p.points(x, y, dx, dy, color, symbol, label) |
---|
575 | |
---|
576 | self.subplot.set_yscale(self.yscale) |
---|
577 | self.subplot.set_xscale(self.xscale) |
---|
578 | |
---|
579 | def interactive_curve(self,x,y,dy=None,name='',color=0,symbol=0,label=None): |
---|
580 | """Draw markers with error bars""" |
---|
581 | self.subplot.set_yscale('linear') |
---|
582 | self.subplot.set_xscale('linear') |
---|
583 | |
---|
584 | from plottable_interactor import PointInteractor |
---|
585 | p = PointInteractor(self, self.subplot, zorder=4, id=name) |
---|
586 | p.curve(x, y, dy, color, symbol, label) |
---|
587 | |
---|
588 | self.subplot.set_yscale(self.yscale) |
---|
589 | self.subplot.set_xscale(self.xscale) |
---|
590 | |
---|
591 | def plottable_selected(self, id): |
---|
592 | """ |
---|
593 | Called to register a plottable as selected |
---|
594 | """ |
---|
595 | #TODO: check that it really is in the list of plottables |
---|
596 | self.graph.selected_plottable = id |
---|
597 | |
---|
598 | def points(self,x,y,dx=None,dy=None,color=0,symbol=0,label=None): |
---|
599 | """Draw markers with error bars""" |
---|
600 | self.subplot.set_yscale('linear') |
---|
601 | self.subplot.set_xscale('linear') |
---|
602 | |
---|
603 | # Convert tuple (lo,hi) to array [(x-lo),(hi-x)] |
---|
604 | if dx != None and type(dx) == type(()): |
---|
605 | dx = nx.vstack((x-dx[0],dx[1]-x)).transpose() |
---|
606 | if dy != None and type(dy) == type(()): |
---|
607 | dy = nx.vstack((y-dy[0],dy[1]-y)).transpose() |
---|
608 | |
---|
609 | if dx==None and dy==None: |
---|
610 | h = self.subplot.plot(x,y,color=self._color(color), |
---|
611 | marker=self._symbol(symbol),linestyle='',label=label) |
---|
612 | else: |
---|
613 | col = self._color(color) |
---|
614 | self.subplot.errorbar(x, y, yerr=dy, xerr=None, |
---|
615 | ecolor=col, capsize=2,linestyle='', barsabove=False, |
---|
616 | mec=col, mfc=col, |
---|
617 | marker=self._symbol(symbol), |
---|
618 | lolims=False, uplims=False, |
---|
619 | xlolims=False, xuplims=False,label=label) |
---|
620 | |
---|
621 | self.subplot.set_yscale(self.yscale) |
---|
622 | self.subplot.set_xscale(self.xscale) |
---|
623 | |
---|
624 | def curve(self,x,y,dy=None,color=0,symbol=0,label=None): |
---|
625 | """Draw a line on a graph, possibly with confidence intervals.""" |
---|
626 | c = self._color(color) |
---|
627 | self.subplot.set_yscale('linear') |
---|
628 | self.subplot.set_xscale('linear') |
---|
629 | |
---|
630 | hlist = self.subplot.plot(x,y,color=c,marker='',linestyle='-',label=label) |
---|
631 | |
---|
632 | self.subplot.set_yscale(self.yscale) |
---|
633 | self.subplot.set_xscale(self.xscale) |
---|
634 | |
---|
635 | def _color(self,c): |
---|
636 | """Return a particular colour""" |
---|
637 | return self.colorlist[c%len(self.colorlist)] |
---|
638 | |
---|
639 | def _symbol(self,s): |
---|
640 | """Return a particular symbol""" |
---|
641 | return self.symbollist[s%len(self.symbollist)] |
---|
642 | |
---|
643 | def _replot(self): |
---|
644 | """ |
---|
645 | Rescale the plottables according to the latest |
---|
646 | user selection and update the plot |
---|
647 | """ |
---|
648 | self.graph.reset_scale() |
---|
649 | self._onEVT_FUNC_PROPERTY(remove_fit=False) |
---|
650 | |
---|
651 | #TODO: Why do we have to have the following line? |
---|
652 | self.fit_result.reset_view() |
---|
653 | |
---|
654 | self.graph.render(self) |
---|
655 | self.subplot.figure.canvas.draw_idle() |
---|
656 | |
---|
657 | def _onEVT_FUNC_PROPERTY(self, remove_fit=True): |
---|
658 | """ |
---|
659 | Receive the x and y transformation from myDialog,Transforms x and y in View |
---|
660 | and set the scale |
---|
661 | """ |
---|
662 | list =[] |
---|
663 | list = self.graph.returnPlottable() |
---|
664 | |
---|
665 | if remove_fit: |
---|
666 | self.fit_result.x =[] |
---|
667 | self.fit_result.y =[] |
---|
668 | self.fit_result.dx=None |
---|
669 | self.fit_result.dy=None |
---|
670 | self.graph.delete(self.fit_result) |
---|
671 | |
---|
672 | for item in list: |
---|
673 | item.setLabel(self.xLabel,self.yLabel) |
---|
674 | if ( self.xLabel=="x" ): |
---|
675 | item.transformX(transform.toX,transform.errToX) |
---|
676 | self.set_xscale("linear") |
---|
677 | name, units = item.get_xaxis() |
---|
678 | self.graph.xaxis("%s" % name, "%s" % units) |
---|
679 | |
---|
680 | |
---|
681 | if ( self.xLabel=="x^(2)" ): |
---|
682 | item.transformX(transform.toX2,transform.errToX2) |
---|
683 | self.set_xscale('linear') |
---|
684 | name, units = item.get_xaxis() |
---|
685 | units=convertUnit(2,units) |
---|
686 | self.graph.xaxis("%s^{2}" % name, "%s" % units) |
---|
687 | |
---|
688 | |
---|
689 | if (self.xLabel=="log10(x)" ): |
---|
690 | item.transformX(transform.toX_pos,transform.errToX_pos) |
---|
691 | self.set_xscale("log") |
---|
692 | name, units = item.get_xaxis() |
---|
693 | self.graph.xaxis("\log_{10}\ \ (%s)" % name, "%s" % units) |
---|
694 | |
---|
695 | |
---|
696 | if ( self.yLabel=="ln(y)" ): |
---|
697 | item.transformY(transform.toLogX,transform.errToLogX) |
---|
698 | self.set_yscale("linear") |
---|
699 | name, units = item.get_yaxis() |
---|
700 | self.graph.yaxis("\log\ \ %s" % name, "%s" % units) |
---|
701 | |
---|
702 | |
---|
703 | if ( self.yLabel=="y" ): |
---|
704 | item.transformY(transform.toX,transform.errToX) |
---|
705 | self.set_yscale("linear") |
---|
706 | name, units = item.get_yaxis() |
---|
707 | self.graph.yaxis("%s" % name, "%s" % units) |
---|
708 | |
---|
709 | |
---|
710 | if ( self.yLabel=="log10(y)" ): |
---|
711 | item.transformY(transform.toX_pos,transform.errToX_pos) |
---|
712 | self.set_yscale("log") |
---|
713 | name, units = item.get_yaxis() |
---|
714 | self.graph.yaxis("\log_{10}\ \ (%s)" % name, "%s" % units) |
---|
715 | |
---|
716 | |
---|
717 | if ( self.yLabel=="y^(2)" ): |
---|
718 | item.transformY( transform.toX2,transform.errToX2 ) |
---|
719 | self.set_yscale("linear") |
---|
720 | name, units = item.get_yaxis() |
---|
721 | units=convertUnit(2,units) |
---|
722 | self.graph.yaxis("%s^{2}" % name, "%s" % units) |
---|
723 | |
---|
724 | |
---|
725 | if ( self.yLabel =="1/y"): |
---|
726 | item.transformY(transform.toOneOverX,transform.errOneOverX ) |
---|
727 | self.set_yscale("linear") |
---|
728 | name, units = item.get_yaxis() |
---|
729 | units=convertUnit(-1,units) |
---|
730 | self.graph.yaxis("1/%s" % name, "%s" % units) |
---|
731 | |
---|
732 | if ( self.yLabel =="1/sqrt(y)" ): |
---|
733 | item.transformY(transform.toOneOverSqrtX,transform.errOneOverSqrtX ) |
---|
734 | self.set_yscale("linear") |
---|
735 | name, units = item.get_yaxis() |
---|
736 | units=convertUnit(-0.5,units) |
---|
737 | self.graph.yaxis("1/\sqrt{%s}" %name, "%s" % units) |
---|
738 | |
---|
739 | if ( self.yLabel =="ln(y*x)"): |
---|
740 | item.transformY( transform.toLogXY,transform.errToLogXY) |
---|
741 | self.set_yscale("linear") |
---|
742 | yname, yunits = item.get_yaxis() |
---|
743 | xname, xunits = item.get_xaxis() |
---|
744 | self.graph.yaxis("\log\ (%s \ \ %s)" % (yname,xname), "%s%s" % (yunits,xunits)) |
---|
745 | |
---|
746 | |
---|
747 | if ( self.yLabel =="ln(y*x^(2))"): |
---|
748 | item.transformY( transform.toLogYX2,transform.errToLogYX2) |
---|
749 | self.set_yscale("linear") |
---|
750 | yname, yunits = item.get_yaxis() |
---|
751 | xname, xunits = item.get_xaxis() |
---|
752 | xunits = convertUnit(2,xunits) |
---|
753 | self.graph.yaxis("\log (%s \ \ %s^{2})" % (yname,xname), "%s%s" % (yunits,xunits)) |
---|
754 | |
---|
755 | |
---|
756 | if ( self.yLabel =="ln(y*x^(4))"): |
---|
757 | item.transformY(transform.toLogYX4,transform.errToLogYX4) |
---|
758 | self.set_yscale("linear") |
---|
759 | yname, yunits = item.get_yaxis() |
---|
760 | xname, xunits = item.get_xaxis() |
---|
761 | xunits = convertUnit(4,xunits) |
---|
762 | self.graph.yaxis("\log (%s \ \ %s^{4})" % (yname,xname), "%s%s" % (yunits,xunits)) |
---|
763 | |
---|
764 | if ( self.viewModel == "Guinier lny vs x^(2)"): |
---|
765 | |
---|
766 | item.transformX(transform.toX2,transform.errToX2) |
---|
767 | self.set_xscale('linear') |
---|
768 | name, units = item.get_xaxis() |
---|
769 | units = convertUnit(2,units) |
---|
770 | self.graph.xaxis("%s^{2}" % name, "%s" % units) |
---|
771 | |
---|
772 | |
---|
773 | item.transformY(transform.toLogX,transform.errToLogX ) |
---|
774 | self.set_yscale("linear") |
---|
775 | name, units = item.get_yaxis() |
---|
776 | self.graph.yaxis("\log\ \ %s" % name, "%s" % units) |
---|
777 | |
---|
778 | |
---|
779 | item.transformView() |
---|
780 | |
---|
781 | |
---|
782 | self.resetFitView() |
---|
783 | self.prevXtrans = self.xLabel |
---|
784 | self.prevYtrans = self.yLabel |
---|
785 | self.graph.render(self) |
---|
786 | self.subplot.figure.canvas.draw_idle() |
---|
787 | |
---|
788 | |
---|
789 | |
---|
790 | def onFitDisplay(self, tempx,tempy,xminView,xmaxView,xmin,xmax,func): |
---|
791 | """ |
---|
792 | Add a new plottable into the graph .In this case this plottable will be used |
---|
793 | to fit some data |
---|
794 | @param tempx: The x data of fit line |
---|
795 | @param tempy: The y data of fit line |
---|
796 | @param xminView: the lower bound of fitting range |
---|
797 | @param xminView: the upper bound of fitting range |
---|
798 | @param xmin: the lowest value of data to fit to the line |
---|
799 | @param xmax: the highest value of data to fit to the line |
---|
800 | """ |
---|
801 | # Saving value to redisplay in Fit Dialog when it is opened again |
---|
802 | self.Avalue,self.Bvalue,self.ErrAvalue,self.ErrBvalue,self.Chivalue=func |
---|
803 | self.xminView=xminView |
---|
804 | self.xmaxView=xmaxView |
---|
805 | self.xmin= xmin |
---|
806 | self.xmax= xmax |
---|
807 | #In case need to change the range of data plotted |
---|
808 | list =[] |
---|
809 | list = self.graph.returnPlottable() |
---|
810 | for item in list: |
---|
811 | #item.onFitRange(xminView,xmaxView) |
---|
812 | item.onFitRange(None,None) |
---|
813 | |
---|
814 | # Create new data plottable with result |
---|
815 | self.fit_result.x =[] |
---|
816 | self.fit_result.y =[] |
---|
817 | self.fit_result.x =tempx |
---|
818 | self.fit_result.y =tempy |
---|
819 | self.fit_result.dx=None |
---|
820 | self.fit_result.dy=None |
---|
821 | #Load the view with the new values |
---|
822 | self.fit_result.reset_view() |
---|
823 | # Add the new plottable to the graph |
---|
824 | self.graph.add(self.fit_result) |
---|
825 | self.graph.render(self) |
---|
826 | self.subplot.figure.canvas.draw_idle() |
---|
827 | |
---|
828 | |
---|
829 | def onResetGraph(self,event): |
---|
830 | """ |
---|
831 | Reset the graph by plotting the full range of data |
---|
832 | """ |
---|
833 | list =[] |
---|
834 | list = self.graph.returnPlottable() |
---|
835 | for item in list: |
---|
836 | item.onReset() |
---|
837 | self.graph.render(self) |
---|
838 | self.subplot.figure.canvas.draw_idle() |
---|
839 | |
---|
840 | class NoRepaintCanvas(FigureCanvasWxAgg): |
---|
841 | """We subclass FigureCanvasWxAgg, overriding the _onPaint method, so that |
---|
842 | the draw method is only called for the first two paint events. After that, |
---|
843 | the canvas will only be redrawn when it is resized. |
---|
844 | """ |
---|
845 | def __init__(self, *args, **kwargs): |
---|
846 | FigureCanvasWxAgg.__init__(self, *args, **kwargs) |
---|
847 | self._drawn = 0 |
---|
848 | |
---|
849 | def _onPaint(self, evt): |
---|
850 | """ |
---|
851 | Called when wxPaintEvt is generated |
---|
852 | """ |
---|
853 | if not self._isRealized: |
---|
854 | self.realize() |
---|
855 | if self._drawn < 2: |
---|
856 | self.draw(repaint = False) |
---|
857 | self._drawn += 1 |
---|
858 | self.gui_repaint(drawDC=wx.PaintDC(self)) |
---|
859 | |
---|