Changeset 482 for trunk/python
- Timestamp:
- 02/18/05 17:40:22 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/asaplot.py
r411 r482 7 7 import Tkinter as Tk 8 8 9 #print "Importing matplotlib with TkAgg backend."10 9 import matplotlib 11 10 matplotlib.use("TkAgg") … … 16 15 from matplotlib.figure import Figure, Text 17 16 from matplotlib.numerix import sqrt 18 from matplotlib import rc, rcParams17 from matplotlib import rc, rcParams 19 18 20 19 # Force use of the newfangled toolbar. … … 29 28 """ 30 29 31 def __init__(self, rows=1, cols=0, title='', size=( 7,5), buffering=False):30 def __init__(self, rows=1, cols=0, title='', size=(8,6), buffering=False): 32 31 """ 33 32 Create a new instance of the ASAPlot plotting class. … … 37 36 """ 38 37 self.window = Tk.Tk() 39 self.is_dead = False 40 def dest_callback(): 41 self.is_dead = True 42 self.window.destroy() 43 44 self.window.protocol("WM_DELETE_WINDOW", dest_callback) 45 #self.frame1 = Tk.Frame(self.window, relief=Tk.RIDGE, borderwidth=4) 46 #self.frame1.pack(fill=Tk.BOTH) 47 48 self.figure = Figure(figsize=size,facecolor='#ddddee') 38 self.is_dead = False 39 def dest_callback(): 40 self.is_dead = True 41 self.window.destroy() 42 43 self.window.protocol("WM_DELETE_WINDOW", dest_callback) 44 45 self.figure = Figure(figsize=size, facecolor='#ddddee') 49 46 self.canvas = FigureCanvasTkAgg(self.figure, master=self.window) 50 47 self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) … … 71 68 matplotlib.interactive = True 72 69 self.buffering = buffering 73 70 74 71 self.canvas.show() 75 72 … … 86 83 self.colours[0] = 1 87 84 self.lines = [] 88 85 89 86 90 87 def delete(self, numbers=None): … … 298 295 299 296 297 def position(self): 298 """ 299 Use the mouse to get a position from a graph. 300 """ 301 302 def position_disable(event): 303 self.register('button_press', None) 304 print '%.4f, %.4f' % (event.xdata, event.ydata) 305 306 print 'Press any mouse button...' 307 self.register('button_press', position_disable) 308 309 300 310 def quit(self): 301 311 """ … … 303 313 """ 304 314 self.window.destroy() 315 316 317 def region(self): 318 """ 319 Use the mouse to get a rectangular region from a plot. 320 321 The return value is [x0, y0, x1, y1] in world coordinates. 322 """ 323 324 def region_start(event): 325 height = self.canvas.figure.bbox.height() 326 self.rect = {'fig': None, 'height': height, 327 'x': event.x, 'y': height - event.y, 328 'world': [event.xdata, event.ydata, 329 event.xdata, event.ydata]} 330 self.register('button_press', None) 331 self.register('motion_notify', region_draw) 332 self.register('button_release', region_disable) 333 334 def region_draw(event): 335 self.canvas._tkcanvas.delete(self.rect['fig']) 336 self.rect['fig'] = self.canvas._tkcanvas.create_rectangle( 337 self.rect['x'], self.rect['y'], 338 event.x, self.rect['height'] - event.y) 339 340 def region_disable(event): 341 self.register('motion_notify', None) 342 self.register('button_release', None) 343 344 self.canvas._tkcanvas.delete(self.rect['fig']) 345 346 self.rect['world'][2:4] = [event.xdata, event.ydata] 347 print '(%.2f, %.2f) (%.2f, %.2f)' % (self.rect['world'][0], 348 self.rect['world'][1], self.rect['world'][2], 349 self.rect['world'][3]) 350 351 self.register('button_press', region_start) 352 353 # This has to be modified to block and return the result (currently 354 # printed by region_disable) when that becomes possible in matplotlib. 355 356 return [0.0, 0.0, 0.0, 0.0] 305 357 306 358 … … 309 361 Register, reregister, or deregister events of type 'button_press', 310 362 'button_release', or 'motion_notify'. 311 363 312 364 The specified callback function should have the following signature: 313 365 … … 371 423 372 424 425 def save(self, fname=None): 426 """ 427 Save the plot to a file. 428 429 fname is the name of the output file. The image format is determined 430 from the file suffix; 'png', 'ps', and 'eps' are recognized. If no 431 file name is specified 'yyyymmdd_hhmmss.png' is created in the current 432 directory. 433 """ 434 if fname is None: 435 from datetime import datetime 436 dstr = datetime.now().strftime('%Y%m%d_%H%M%S') 437 fname = 'asap'+dstr+'.png' 438 439 d = ['png','.ps','eps'] 440 441 from os.path import expandvars 442 fname = expandvars(fname) 443 444 if fname[-3:].lower() in d: 445 try: 446 self.canvas.print_figure(fname) 447 print 'Written file %s' % (fname) 448 except IOError, msg: 449 print 'Failed to save %s: Error msg was\n\n%s' % (fname, err) 450 return 451 else: 452 print "Invalid image type. Valid types are:" 453 print "ps, eps, png" 454 455 373 456 def set_axes(self, what=None, *args, **kwargs): 374 457 """ … … 419 502 getattr(self.figure, "set_%s"%what)(*args, **newargs) 420 503 self.show() 504 505 506 def set_limits(self, xlim=None, ylim=None): 507 """ 508 Set x-, and y-limits for each subplot. 509 510 xlim = [xmin, xmax] as in axes.set_xlim(). 511 ylim = [ymin, ymax] as in axes.set_ylim(). 512 """ 513 514 for s in self.subplots: 515 self.axes = s['axes'] 516 self.lines = s['lines'] 517 if xlim is not None: 518 self.axes.set_xlim(xlim) 519 if ylim is not None: 520 self.axes.set_ylim(ylim) 521 return 421 522 422 523 … … 469 570 """ 470 571 Set the panel layout. 471 572 472 573 rows and cols, if cols != 0, specify the number of rows and columns in 473 574 a regular layout. (Indexing of these panels in matplotlib is row- … … 483 584 non-regular panel layouts to be constructed via multiple calls. Any 484 585 other value of n clears the plot and produces a rectangular array of 485 empty panels. 586 empty panels. The number of these may be limited by nplots. 486 587 """ 487 588 if n < 0 and len(self.subplots): … … 489 590 self.set_title() 490 591 491 if rows < 1: 492 rows = 1 493 nel = 1 494 if cols == 0: 495 nel = rows 592 if rows < 1: rows = 1 593 594 if cols <= 0: 496 595 i = int(sqrt(rows)) 497 596 if i*i < rows: i += 1 … … 500 599 if i*(i-1) >= rows: i -= 1 501 600 rows = i 502 else: 503 if nplots > -1: 504 nel=nplots 601 505 602 if 0 <= n < rows*cols: 506 603 i = len(self.subplots) 507 604 self.subplots.append({}) 605 508 606 self.subplots[i]['axes'] = self.figure.add_subplot(rows, 509 607 cols, n+1) … … 512 610 if i == 0: self.subplot(0) 513 611 612 self.rows = 0 613 self.cols = 0 614 514 615 else: 515 616 self.subplots = [] 516 for i in range(nel): 617 618 if nplots < 1 or rows*cols < nplots: 619 nplots = rows*cols 620 621 for i in range(nplots): 517 622 self.subplots.append({}) 623 518 624 self.subplots[i]['axes'] = self.figure.add_subplot(rows, 519 625 cols, i+1) 520 626 self.subplots[i]['lines'] = [] 521 627 628 if rows > 1 or cols > 1: 629 # Squeeze the plots together. 630 pos = self.subplots[i]['axes'].get_position() 631 if cols > 1: pos[2] *= 1.1 632 if rows > 1: pos[3] *= 1.1 633 self.subplots[i]['axes'].set_position(pos) 634 635 # Suppress tick labelling for interior subplots. 636 if i <= (rows-1)*cols - 1: 637 # Suppress x-labels for frames not in the bottom row. 638 self.subplots[i]['axes'].set_xticklabels([]) 639 640 if i%cols: 641 # Suppress y-labels for frames not in the left column. 642 self.subplots[i]['axes'].set_yticklabels([]) 643 644 self.rows = rows 645 self.cols = cols 646 522 647 self.subplot(0) 523 648 … … 540 665 if not self.buffering: 541 666 if self.loc: 542 for j in range(len(self.subplots)): 543 lines = [] 544 labels = [] 545 i = 0 546 for line in self.subplots[j]['lines']: 547 i += 1 548 if line is not None: 549 lines.append(line[0]) 550 lbl = line[0].get_label() 551 if lbl == '': 552 lbl = str(i) 553 labels.append(lbl) 554 555 if len(lines): 556 self.subplots[j]['axes'].legend(tuple(lines), tuple(labels), self.loc) 557 else: 558 self.subplots[j]['axes'].legend((' ')) 667 for j in range(len(self.subplots)): 668 lines = [] 669 labels = [] 670 i = 0 671 for line in self.subplots[j]['lines']: 672 i += 1 673 if line is not None: 674 lines.append(line[0]) 675 lbl = line[0].get_label() 676 if lbl == '': 677 lbl = str(i) 678 labels.append(lbl) 679 680 if len(lines): 681 self.subplots[j]['axes'].legend(tuple(lines), 682 tuple(labels), 683 self.loc) 684 else: 685 self.subplots[j]['axes'].legend((' ')) 559 686 560 687 self.window.wm_deiconify() … … 592 719 """ 593 720 self.figure.text(*args, **kwargs) 594 self.show() 721 self.show() 722 595 723 596 724 def unmap(self): … … 599 727 """ 600 728 self.window.wm_withdraw() 601 602 def set_limits(self,xlim=None,ylim=None):603 for s in self.subplots:604 self.axes = s['axes']605 self.lines = s['lines']606 if xlim is not None:607 self.axes.set_xlim(xlim)608 if ylim is not None:609 self.axes.set_ylim(ylim)610 return611 612 def save(self, fname=None):613 """614 Save the plot to a file. The know formats are 'png', 'ps', 'eps'.615 Parameters:616 filename: The name of the output file. This is optional617 and autodetects the image format from the file618 suffix. If non filename is specified a file619 called 'yyyymmdd_hhmmss.png' is created in the620 current directory.621 """622 if fname is None:623 from datetime import datetime624 dstr = datetime.now().strftime('%Y%m%d_%H%M%S')625 fname = 'asap'+dstr+'.png'626 627 d = ['png','.ps','eps']628 629 from os.path import expandvars630 fname = expandvars(fname)631 632 if fname[-3:].lower() in d:633 try:634 self.canvas.print_figure(fname)635 print 'Written file %s' % (fname)636 except IOError, msg:637 print 'Failed to save %s: Error msg was\n\n%s' % (fname, err)638 return639 else:640 print "Invalid image type. Valid types are:"641 print "ps, eps, png"642 729 643 730 … … 684 771 Load the colour dictionary from the specified file. 685 772 """ 686 print 'Loading colour dictionary from', file 773 print 'Loading colour dictionary from', filename 687 774 from os.path import expandvars 688 775 filename = expandvars(filename)
Note:
See TracChangeset
for help on using the changeset viewer.