Changeset 2234 for trunk/GSASIIplot.py
- Timestamp:
- May 1, 2016 10:46:45 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIplot.py
r2216 r2234 75 75 self.ReplotArgs = [] 76 76 self.ReplotKwArgs = {} 77 self.needsUpdate = True78 77 wx.Panel.__init__(self,parent,id=id,**kwargs) 79 78 80 def AddRefresh(self,ReplotRoutine,ReplotArgs=[],ReplotKwArgs={}):81 '''Define a routine used to refresh the plot82 83 :param function ReplotRoutine: function to be called84 :param list ReplotArgs: list of positional parameters, if any85 :param dict ReplotKwArgs: dict of keyword parameters, if any86 '''87 self.ReplotRoutine = ReplotRoutine88 self.ReplotArgs = ReplotArgs89 self.ReplotKwArgs = ReplotKwArgs90 91 def Refresh(self):92 'Replots the selected tab'93 if self.ReplotRoutine:94 #print 'Refresh G2PlotMpl with',self.ReplotRoutine95 if not self.needsUpdate:96 #print 'already updated\n'97 return True98 #if self.ReplotArgs: print '...args',self.ReplotArgs99 #if self.ReplotKwArgs: print '...KWargs',self.ReplotKwArgs100 self.ReplotRoutine(*self.ReplotArgs,**self.ReplotKwArgs)101 return True102 else:103 return False104 79 class G2PlotMpl(_tabPlotWin): 105 80 'Creates a Matplotlib 2-D plot in the GSAS-II graphics window' … … 156 131 class G2PlotNoteBook(wx.Panel): 157 132 'create a tabbed window for GSAS-II graphics' 158 def __init__(self,parent,id=-1 ):133 def __init__(self,parent,id=-1,G2frame=None): 159 134 wx.Panel.__init__(self,parent,id=id) 160 135 #so one can't delete a plot page from tab!! … … 169 144 self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.OnPageChanged) 170 145 self.nb.Bind(wx.EVT_KEY_UP,self.OnNotebookKey) 146 self.G2frame = G2frame 171 147 172 148 self.plotList = [] # contains the tab label for each plot 173 149 self.panelList = [] # contains the panel object for each plot 174 150 self.figList = [] # contains the figure object for each plot 175 self.pageOnTop = None # keep track of top page during refresh all 176 self.skipPageChange = False 151 self.treeItem = {} # contains the tree entry that generated a plot, indexed by the plot number 152 self.skipPageChange = False # set to True when no plot update is needed 153 self.allowZoomReset = True # this indicates plot should be updated not initialized 177 154 178 155 def OnNotebookKey(self,event): … … 202 179 self.figList.append(page.figure) # figure object for plot 203 180 self.skipPageChange = False 204 205 def registerReplot(self,name,ReplotRoutine,ReplotArgs=[],ReplotKwArgs={}): 206 'Define the routine and args needed to replot a figure' 207 try: 208 plotNum = self.plotList.index(name) 209 except ValueError: 210 print('No plot tab labeled '+name) 211 GSASIIpath.IPyBreak() 212 page = self.panelList[plotNum] 213 page.AddRefresh(ReplotRoutine,ReplotArgs,ReplotKwArgs) 214 215 def setReplotFlags(self): 216 'Flag all plots as needing a redraw' 217 for page in self.panelList: 218 page.needsUpdate = True 219 self.pageOnTop = self.nb.GetSelection() 220 221 def clearReplotFlag(self,name): 222 'Set when redrawing a plot so that it is not done twice' 223 try: 224 plotNum = self.plotList.index(name) 225 except ValueError: 226 print('clearReplotFlag: No plot tab labeled '+name) 227 page = self.panelList[plotNum] 228 page.needsUpdate = False 229 230 def ResetOnTop(self): 231 'Put the saved page back on top' 232 self.SetSelectionNoRefresh(self.pageOnTop) 233 234 def SetSelectionNoRefresh(self,plotNum): 235 'Raises a plot tab without triggering a refresh via OnPageChanged' 236 self.skipPageChange = True 237 self.nb.SetSelection(plotNum) # raises plot tab 238 self.skipPageChange = False 239 181 self._registerTreeItem(name) 182 183 def _registerTreeItem(self,plotLabel): 184 '''Save the name of the of the data tree item that has generated the plot 185 and for phases, save the name of the generating tab (at present that is not used) 186 ''' 187 tree = self.G2frame.PatternTree 188 self.treeItem[plotLabel] = [tree._getTreeItemsList(tree.GetSelection()),''] 189 if len(self.treeItem[plotLabel]) == 2 and self.treeItem[plotLabel][0][0] == 'Phases': 190 self.treeItem[plotLabel][1] = self.G2frame.dataDisplayPhaseText 191 240 192 def RaisePageNoRefresh(self,Page): 241 193 'Raises a plot tab without triggering a refresh via OnPageChanged' … … 243 195 Page.SetFocus() 244 196 self.skipPageChange = False 245 246 def replotAll(self): 247 'refresh all current plots, if not already redrawn' 248 for page,label in zip(self.panelList,self.plotList): 249 if page.Refresh(): 250 pass 251 elif GSASIIpath.GetConfigValue('debug'): 252 print('No refresh for '+label) 253 if self.pageOnTop is not None: 254 wx.CallLater(150,self.ResetOnTop) 255 197 198 def SetSelectionNoRefresh(self,plotNum): 199 'Raises a plot tab without triggering a refresh via OnPageChanged' 200 self.skipPageChange = True 201 self.nb.SetSelection(plotNum) # raises plot tab 202 Page = self.G2frame.G2plotNB.nb.GetPage(plotNum) 203 Page.SetFocus() 204 self.skipPageChange = False 205 256 206 def addMpl(self,name=""): 257 207 'Add a tabbed page with a matplotlib plot' … … 313 263 self.status.SetStatusWidths([150,-1]) 314 264 if self.skipPageChange: 315 # print 'skipping OnPageChanged'316 265 self.skipPageChange = False 317 266 return 318 # print 'OnPageChanged'319 267 page = self.panelList[self.nb.GetSelection()] #GetCurrentPage() not in wx 2.7 320 page.needsUpdate = True 321 if page.Refresh(): # refresh plot, if possible 322 pass 323 elif self.plotList: 324 self.status.SetStatusText('Better to select this from GSAS-II data tree',1) 325 268 tabLabel = event.GetEventObject().GetPageText(event.GetSelection()) 269 if tabLabel in self.treeItem: 270 treeItems, tabname = self.treeItem[tabLabel] 271 id = self.G2frame.root 272 for item in treeItems: 273 id = G2gd.GetPatternTreeItemId(self.G2frame, id, item) 274 wx.CallLater(100,self.InvokeTreeItem,id) 275 else: 276 print 'OnPageChanged: not found:',tabLabel 277 278 def InvokeTreeItem(self,id): 279 '''This is called to select an item from the tree using the self.allowZoomReset 280 flag to prevent a reset to the zoom of the plot (where implemented) 281 ''' 282 self.allowZoomReset = False 283 self.G2frame.PatternTree.SelectItem(id) 284 self.allowZoomReset = True 285 326 286 class GSASIItoolbar(Toolbar): 327 287 'Override the matplotlib toolbar so we can add more icons' … … 1161 1121 ################################################################################ 1162 1122 1163 def PlotPatterns(G2frame,newPlot=False,plotType='PWDR' ,TreeItemText=None):1123 def PlotPatterns(G2frame,newPlot=False,plotType='PWDR'): 1164 1124 '''Powder pattern plotting package - displays single or multiple powder patterns as intensity vs 1165 1125 2-theta, q or TOF. Can display multiple patterns as "waterfall plots" or contour plots. Log I 1166 1126 plotting available. 1167 1168 The histogram to be plotted is found in G2frame.PatternId unless the histogram name is specified1169 as TreeItemText1170 1127 ''' 1171 1128 global exclLines … … 1174 1131 global Pattern 1175 1132 plottype = plotType 1176 if TreeItemText:1177 G2frame.PatternId = G2gd.GetPatternTreeItemId(G2frame,G2frame.root,TreeItemText)1178 else:1179 TreeItemText = G2frame.PatternTree.GetItemText(G2frame.PatternId)1180 1133 1181 1134 if not G2frame.PatternId: … … 1654 1607 Page.canvas.mpl_connect('button_release_event', OnRelease) 1655 1608 Page.canvas.mpl_connect('button_press_event',OnPress) 1656 G2frame.G2plotNB.registerReplot('Powder Patterns',1657 ReplotRoutine=PlotPatterns,1658 ReplotKwArgs={'G2frame':G2frame, 'plotType':plottype,1659 'TreeItemText':TreeItemText, 'newPlot':True1660 })1661 G2frame.G2plotNB.status.SetStatusText('histogram: '+TreeItemText,1)1662 G2frame.G2plotNB.clearReplotFlag('Powder Patterns')1663 1609 # if plottype == 'PWDR': # avoids a very nasty clash with KILL_FOCUS in SASD TextCtrl? 1664 1610 # Page.SetFocus() … … 2849 2795 ################################################################################ 2850 2796 2851 def PlotPeakWidths(G2frame ,TreeItemText=None):2797 def PlotPeakWidths(G2frame): 2852 2798 ''' Plotting of instrument broadening terms as function of 2-theta 2853 2799 Seen when "Instrument Parameters" chosen from powder pattern data tree … … 2857 2803 # gamFW = lambda s,g: np.exp(np.log(s**5+2.69269*s**4*g+2.42843*s**3*g**2+4.47163*s**2*g**3+0.07842*s*g**4+g**5)/5.) 2858 2804 # gamFW2 = lambda s,g: math.sqrt(s**2+(0.4654996*g)**2)+.5345004*g #Ubaldo Bafile - private communication 2859 if TreeItemText:2860 G2frame.PatternId = G2gd.GetPatternTreeItemId(G2frame,G2frame.root,TreeItemText)2861 else:2862 TreeItemText = G2frame.PatternTree.GetItemText(G2frame.PatternId)2863 2805 PatternId = G2frame.PatternId 2864 2806 limitID = G2gd.GetPatternTreeItemId(G2frame,PatternId, 'Limits') … … 2885 2827 print "done" 2886 2828 return 2829 xylim = [] 2887 2830 try: 2888 2831 plotNum = G2frame.G2plotNB.plotList.index('Peak Widths') 2889 2832 Page = G2frame.G2plotNB.nb.GetPage(plotNum) 2833 Plot = Page.figure.gca() 2834 if not G2frame.G2plotNB.allowZoomReset: # save previous limits 2835 xylim = Plot.get_xlim(),Plot.get_ylim() 2890 2836 Page.figure.clf() 2891 2837 Plot = Page.figure.gca() … … 2895 2841 plotNum = G2frame.G2plotNB.plotList.index('Peak Widths') 2896 2842 Page = G2frame.G2plotNB.nb.GetPage(plotNum) 2897 G2frame.G2plotNB.registerReplot('Peak Widths', 2898 ReplotRoutine=PlotPeakWidths, 2899 ReplotKwArgs={'G2frame':G2frame, 'TreeItemText':TreeItemText}) 2843 2844 TreeItemText = G2frame.PatternTree.GetItemText(G2frame.PatternId) 2900 2845 G2frame.G2plotNB.status.SetStatusText('histogram: '+TreeItemText,1) 2901 G2frame.G2plotNB.clearReplotFlag('Peak Widths')2902 2846 Page.Choice = None 2903 2847 G2frame.G2plotNB.RaisePageNoRefresh(Page) … … 3019 2963 Plot.plot(Q,G,'+',color='m',label='Lorentzian peak') 3020 2964 Plot.legend(loc='best') 2965 if xylim and not G2frame.G2plotNB.allowZoomReset: 2966 # this restores previous plot limits (but I'm not sure why there are two .push_current calls) 2967 Page.toolbar.push_current() 2968 Plot.set_xlim(xylim[0]) 2969 Plot.set_ylim(xylim[1]) 2970 Page.toolbar.push_current() 2971 Page.toolbar.draw() 2972 else: 3021 2973 Page.canvas.draw() 3022 2974
Note: See TracChangeset
for help on using the changeset viewer.