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