- Timestamp:
- 12/02/04 16:29:49 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/asaplot.py
r114 r117 13 13 from matplotlib.backends import new_figure_manager, show 14 14 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, \ 15 15 FigureManagerTkAgg 16 16 from matplotlib.figure import Figure, Text 17 17 … … 28 28 29 29 def __init__(self, rowcol='11', title='', size=(8,6), buffering=False): 30 """ 31 Create a new instance of the ASAPlot plotting class. 32 """ 33 self.window = Tk.Tk() 34 35 self.frame1 = Tk.Frame(self.window, relief=Tk.RIDGE, borderwidth=4) 36 self.frame1.pack(fill=Tk.BOTH) 37 38 self.figure = Figure(figsize=size, facecolor='#ddddee') 39 self.canvas = FigureCanvasTkAgg(self.figure, master=self.frame1) 40 self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) 41 42 ## # Simply instantiating this is enough to get a working toolbar. 43 self.figmgr = FigureManagerTkAgg(self.canvas, 1, self.window) 44 self.window.wm_title('ASAPlot graphics window') 45 46 ## self.frame2 = Tk.Frame(self.window, relief=Tk.RIDGE, borderwidth=4) 47 ## self.frame2.pack(fill=Tk.X) 48 ## self.label1 = Tk.Label(self.frame2, width=10) 49 ## self.label1.pack(side=Tk.LEFT, fill=Tk.X) 50 ## self.label2 = Tk.Label(self.frame2) 51 ## self.label2.pack(side=Tk.LEFT, fill=Tk.X, expand=1) 52 53 ## self.button = Tk.Button(master=self.frame2, text='Dismiss', 54 ## command=self.quit) 55 ## self.button.pack(side=Tk.RIGHT) 56 57 self.figure.text(0.5, 0.95, title, horizontalalignment='center') 58 59 self.rows = int(rowcol[0]) 60 self.cols = int(rowcol[1]) 61 self.subplots = [] 62 for i in range(0,self.rows*self.cols): 63 self.subplots.append({}) 64 self.subplots[i]['axes'] = self.figure.add_subplot(self.rows, 65 self.cols, i+1) 66 self.subplots[i]['lines'] = [] 67 68 ## def mouse_move(event): 69 ## for i in range(len(self.subplots)): 70 ## axes = self.subplots[i]['axes'] 71 ## height = axes.figure.bbox.height() 72 ## x, y = event.x, height-event.y 73 74 ## if axes.in_axes(x, y): 75 ## # The cursor position. 76 ## x, y = axes.transData.inverse_xy_tup((x,y)) 77 ## self.label2.config(text='x=%1.2f, y=%1.2f'%(x,y)) 78 ## return 79 ## self.label2.config(text='') 80 81 ## self.canvas.get_tk_widget().bind("<Motion>", mouse_move) 82 self.figmgr.toolbar.set_active([0]) 83 84 self.axes = self.subplots[0]['axes'] 85 self.lines = self.subplots[0]['lines'] 86 87 88 # Set matplotlib default colour sequence. 89 self.colours = [1, 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'] 90 self.attributes = {} 91 self.loc = 1 92 93 matplotlib.interactive = True 94 self.buffering = buffering 95 96 #self.figmgr.show() 97 self.canvas.show() 30 """ 31 Create a new instance of the ASAPlot plotting class. 32 """ 33 self.window = Tk.Tk() 34 35 self.frame1 = Tk.Frame(self.window, relief=Tk.RIDGE, borderwidth=4) 36 self.frame1.pack(fill=Tk.BOTH) 37 38 self.figure = Figure(figsize=size, facecolor='#ddddee') 39 self.canvas = FigureCanvasTkAgg(self.figure, master=self.frame1) 40 self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) 41 42 # Simply instantiating this is enough to get a working toolbar. 43 self.figmgr = FigureManagerTkAgg(self.canvas, 1, self.window) 44 self.window.wm_title('ASAPlot graphics window') 45 46 self.figure.text(0.5, 0.95, title, horizontalalignment='center') 47 48 self.rows = int(rowcol[0]) 49 self.cols = int(rowcol[1]) 50 self.subplots = [] 51 for i in range(0,self.rows*self.cols): 52 self.subplots.append({}) 53 self.subplots[i]['axes'] = self.figure.add_subplot(self.rows, 54 self.cols, i+1) 55 self.subplots[i]['lines'] = [] 56 57 self.figmgr.toolbar.set_active([0]) 58 59 self.axes = self.subplots[0]['axes'] 60 self.lines = self.subplots[0]['lines'] 61 62 63 # Set matplotlib default colour sequence. 64 self.colours = [1, 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'] 65 self.attributes = {} 66 self.loc = 1 67 68 matplotlib.interactive = True 69 self.buffering = buffering 70 71 self.canvas.show() 98 72 99 73 100 74 def clear(self): 101 """ 102 Delete all lines from the plot. Line numbering will restart from 1. 103 """ 104 105 for i in range(1,len(self.lines)+1): 106 self.delete(i) 107 self.axes.clear() 108 self.colours[0] = 1 109 self.lines = [] 110 75 """ 76 Delete all lines from the plot. Line numbering will restart from 1. 77 """ 78 79 for i in range(1,len(self.lines)+1): 80 self.delete(i) 81 82 self.axes.clear() 83 self.colours[0] = 1 84 self.lines = [] 85 111 86 112 87 def delete(self, numbers=None): 113 114 Delete the 1-relative line number, default is to delete the last.115 116 117 118 if numbers is None: numbers = [len(self.lines)]119 120 121 122 123 124 if 0 < number <=len(self.lines):125 if self.lines[number-1] is not None:126 for line in self.lines[number-1]:127 128 self.lines[number-1] = None129 130 88 """ 89 Delete the 0-relative line number, default is to delete the last. 90 The remaining lines are NOT renumbered. 91 """ 92 93 if numbers is None: numbers = [len(self.lines)-1] 94 95 if not hasattr(numbers, '__iter__'): 96 numbers = [numbers] 97 98 for number in numbers: 99 if 0 <= number < len(self.lines): 100 if self.lines[number] is not None: 101 for line in self.lines[number]: 102 line.set_linestyle('None') 103 self.lines[number] = None 104 105 self.show() 131 106 132 107 133 108 def get_line(self): 134 135 136 137 109 """ 110 Get the current default line attributes. 111 """ 112 return self.attributes 138 113 139 114 140 115 def hold(self, hold=True): 141 """ 142 Buffer graphics until subsequently released. 143 """ 144 self.buffering = hold 145 ## if self.buffering: 146 ## self.label1.config(text='On hold') 147 ## else: 148 ## self.label1.config(text='') 116 """ 117 Buffer graphics until subsequently released. 118 """ 119 self.buffering = hold 149 120 150 121 151 122 def legend(self, loc=1): 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 123 """ 124 Add a legend to the plot. 125 126 Any other value for loc else disables the legend: 127 1: upper right 128 2: upper left 129 3: lower left 130 4: lower right 131 5: right 132 6: center left 133 7: center right 134 8: lower center 135 9: upper center 136 10: center 137 138 """ 139 if 1 > loc > 10: loc = 0 140 self.loc = loc 141 self.show() 171 142 172 143 173 144 def map(self): 174 """ 175 Reveal the ASAPlot graphics window and bring it to the top of the 176 window stack. 177 """ 178 self.window.wm_deiconify() 179 self.window.lift() 145 """ 146 Reveal the ASAPlot graphics window and bring it to the top of the 147 window stack. 148 """ 149 self.window.wm_deiconify() 150 self.window.lift() 151 180 152 181 153 def palette(self, pen=None, colours=None): 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 154 """ 155 Redefine the colour sequence. 156 157 pen is the pen number to use for the next plot; this will be auto- 158 incremented. 159 160 colours is the list of pen colours. Colour may be specified via 161 the single letter values understood by matplotlib: 162 163 b: blue 164 g: green 165 r: red 166 c: cyan 167 m: magenta 168 y: yellow 169 k: black 170 w: white 171 172 or via the full name as listed in the colour dictionary which is 173 loaded by default by load_colours() from rgb.txt and listed by 174 list_colours(). 175 """ 176 177 if pen is None and colours is None: 178 self.colours = [] 179 return 180 181 if pen is None: 182 if not len(self.colours): 183 self.colours = [1] 184 else: 185 self.colours[0] = pen 186 187 if colours is None: 188 return 189 190 cols = [] 191 for col in colours: 192 cols.append(get_colour(col)) 193 194 self.colours[1:] = cols 195 196 if 0 > self.colours[0] > len(self.colours): 197 self.colours[0] = 1 226 198 227 199 228 200 def plot(self, x=None, y=None, mask=None, fmt=None, add=None): 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 201 """ 202 Plot the next line in the current frame using the current line 203 attributes. The ASAPlot graphics window will be mapped and raised. 204 205 The argument list works a bit like the matlab plot() function. 206 """ 207 208 if x is None: 209 if y is None: return 210 x = range(len(y)) 211 212 elif y is None: 213 y = x 214 x = range(len(y)) 215 216 if mask is None: 217 if fmt is None: 218 line = self.axes.plot(x, y) 219 else: 220 line = self.axes.plot(x, y, fmt) 221 else: 222 segments = [] 223 224 mask = list(mask) 225 i = 0 226 while mask[i:].count(1): 227 i += mask[i:].index(1) 228 if mask[i:].count(0): 229 j = i + mask[i:].index(0) 230 else: 231 j = len(mask) 232 233 segments.append(x[i:j]) 234 segments.append(y[i:j]) 235 236 i = j 237 238 line = self.axes.plot(*segments) 239 240 # Add to an existing line? 241 if add is None or len(self.lines) < add < 0: 242 self.lines.append(line) 243 i = len(self.lines) - 1 244 else: 245 if add == 0: add = len(self.lines) 246 i = add - 1 247 self.lines[i].extend(line) 248 249 # Set/reset attributes for the line. 250 gotcolour = False 251 for k, v in self.attributes.iteritems(): 252 if k == 'color': gotcolour = True 253 for segment in self.lines[i]: 254 getattr(segment, "set_%s"%k)(v) 255 256 if not gotcolour and len(self.colours): 257 for segment in self.lines[i]: 258 getattr(segment, "set_color")(self.colours[self.colours[0]]) 259 260 self.colours[0] += 1 261 if self.colours[0] >= len(self.colours): 262 self.colours[0] = 1 263 264 self.show() 293 265 294 266 295 267 def quit(self): 296 297 298 299 268 """ 269 Destroy the ASAPlot graphics window. 270 """ 271 self.window.destroy() 300 272 301 273 302 274 def release(self): 303 304 305 306 307 275 """ 276 Release buffered graphics. 277 """ 278 self.buffering = False 279 self.show() 308 280 309 281 310 282 def set_axes(self, what=None, *args, **kwargs): 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 283 """ 284 Set attributes for the axes by calling the relevant Axes.set_*() 285 method. Colour translation is done as described in the doctext 286 for palette(). 287 """ 288 289 if what is None: return 290 if what[-6:] == 'colour': what = what[:-6] + 'color' 291 292 newargs = {} 293 for k, v in kwargs.iteritems(): 294 k = k.lower() 295 if k == 'colour': k = 'color' 296 297 if k == 'color': 298 v = get_colour(v) 299 300 newargs[k] = v 301 302 getattr(self.axes, "set_%s"%what)(*args, **newargs) 303 self.show() 332 304 333 305 334 306 def set_figure(self, what=None, *args, **kwargs): 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 307 """ 308 Set attributes for the figure by calling the relevant Figure.set_*() 309 method. Colour translation is done as described in the doctext 310 for palette(). 311 """ 312 313 if what is None: return 314 if what[-6:] == 'colour': what = what[:-6] + 'color' 315 if what[-5:] == 'color' and len(args): 316 args = (get_colour(args[0]),) 317 318 newargs = {} 319 for k, v in kwargs.iteritems(): 320 k = k.lower() 321 if k == 'colour': k = 'color' 322 323 if k == 'color': 324 v = get_colour(v) 325 326 newargs[k] = v 327 328 getattr(self.figure, "set_%s"%what)(*args, **newargs) 329 self.show() 358 330 359 331 360 332 def set_line(self, number=None, **kwargs): 361 362 363 364 365 number is the 1-relative number of a line that has already been366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 if 0 < number <=len(self.lines):392 if self.lines[number-1] is not None:393 for line in self.lines[number-1]:394 395 396 397 398 399 400 401 402 333 """ 334 Set attributes for the specified line, or else the next line(s) 335 to be plotted. 336 337 number is the 0-relative number of a line that has already been 338 plotted. If no such line exists, attributes are recorded and used 339 for the next line(s) to be plotted. 340 341 Keyword arguments specify Line2D attributes, e.g. color='r'. Do 342 343 import matplotlib 344 help(matplotlib.lines) 345 346 The set_* methods of class Line2D define the attribute names and 347 values. For non-US usage, "colour" is recognized as synonymous with 348 "color". 349 350 Set the value to None to delete an attribute. 351 352 Colour translation is done as described in the doctext for palette(). 353 """ 354 355 redraw = False 356 for k, v in kwargs.iteritems(): 357 k = k.lower() 358 if k == 'colour': k = 'color' 359 360 if k == 'color': 361 v = get_colour(v) 362 363 if 0 <= number < len(self.lines): 364 if self.lines[number] is not None: 365 for line in self.lines[number]: 366 getattr(line, "set_%s"%k)(v) 367 redraw = True 368 else: 369 if v is None: 370 del self.attributes[k] 371 else: 372 self.attributes[k] = v 373 374 if redraw: self.show() 403 375 404 376 405 377 def show(self): 406 """ 407 Show graphics dependent on the current buffering state. 408 """ 409 if not self.buffering: 410 if self.loc: 411 lines = [] 412 labels = [] 413 i = 0 414 for line in self.lines: 415 i += 1 416 if line is not None: 417 lines.append(line[0]) 418 lbl = line[0].get_label() 419 if lbl == '': 420 lbl = str(i) 421 labels.append(lbl) 422 423 if len(lines): 424 self.axes.legend(tuple(lines), tuple(labels), self.loc) 425 else: 426 self.axes.legend((' ')) 427 428 self.window.wm_deiconify() 429 self.canvas.show() 430 431 def subplot(self, col=1, row=1): 432 """ 433 Set the subplot. 434 """ 435 i = (self.cols*(row-1) + col-1)%(self.rows*self.cols) 436 self.axes = self.subplots[i]['axes'] 437 self.lines = self.subplots[i]['lines'] 378 """ 379 Show graphics dependent on the current buffering state. 380 """ 381 if not self.buffering: 382 if self.loc: 383 lines = [] 384 labels = [] 385 i = 0 386 for line in self.lines: 387 i += 1 388 if line is not None: 389 lines.append(line[0]) 390 lbl = line[0].get_label() 391 if lbl == '': 392 lbl = str(i) 393 labels.append(lbl) 394 395 if len(lines): 396 self.axes.legend(tuple(lines), tuple(labels), self.loc) 397 else: 398 self.axes.legend((' ')) 399 400 self.window.wm_deiconify() 401 self.canvas.show() 402 403 404 def subplot(self, col=0, row=0): 405 """ 406 Set the subplot; 0-relative column and row numbers. Overrange column 407 numbers map onto successive rows. 408 """ 409 i = (self.cols*row + col)%(self.rows*self.cols) 410 self.axes = self.subplots[i]['axes'] 411 self.lines = self.subplots[i]['lines'] 438 412 439 413 440 414 def terminate(self): 441 442 443 444 415 """ 416 Clear the figure. 417 """ 418 self.window.destroy() 445 419 446 420 447 421 def text(self, *args, **kwargs): 448 449 450 451 452 422 """ 423 Add text to the figure. 424 """ 425 self.figure.text(*args, **kwargs) 426 self.show() 453 427 454 428 455 429 def unmap(self): 456 457 458 459 430 """ 431 Hide the ASAPlot graphics window. 432 """ 433 self.window.wm_withdraw() 460 434 461 435 … … 479 453 colour = colour.replace('gray','grey') 480 454 for name in colours.keys(): 481 482 455 if name.lower() == colour: 456 return colours[name] 483 457 484 458 return '#000000' … … 495 469 names.sort() 496 470 for name in names: 497 471 print colours[name], name 498 472 499 473 … … 506 480 507 481 while True: 508 509 510 511 512 513 514 515 516 517 518 482 line = rgb.readline() 483 if line == '': break 484 tmp = line.split() 485 486 if len(tmp) == 4: 487 if tmp[3][:4] == 'gray': continue 488 if tmp[3].lower().find('gray') != -1: continue 489 490 name = tmp[3][0].upper() + tmp[3][1:] 491 r, g, b = int(tmp[0]), int(tmp[1]), int(tmp[2]) 492 colours[name] = '#%2.2x%2.2x%2.2x' % (r, g, b)
Note:
See TracChangeset
for help on using the changeset viewer.