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