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