Ignore:
Timestamp:
12/03/04 16:38:12 (19 years ago)
Author:
cal103
Message:

Added general control of the panel layout.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asaplot.py

    r117 r118  
    1515        FigureManagerTkAgg
    1616from matplotlib.figure import Figure, Text
     17from matplotlib.numerix import sqrt
    1718
    1819# Force use of the newfangled toolbar.
     
    2728    """
    2829
    29     def __init__(self, rowcol='11', title='', size=(8,6), buffering=False):
     30    def __init__(self, rows=1, cols=0, title='', size=(8,6), buffering=False):
    3031        """
    3132        Create a new instance of the ASAPlot plotting class.
     
    4445        self.window.wm_title('ASAPlot graphics window')
    4546
    46         self.figure.text(0.5, 0.95, title, horizontalalignment='center')
    47 
    48         self.rows = int(rowcol[0])
    49         self.cols = int(rowcol[1])
     47        self.set_title(title)
    5048        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']
     49        if rows > 0:
     50            self.set_panels(rows, cols)
    6151
    6252
     
    373363
    374364        if redraw: self.show()
     365
     366
     367    def set_panels(self, rows=1, cols=0, n=-1):
     368        """
     369        Set the panel layout.
     370       
     371        rows and cols, if cols != 0, specify the number of rows and columns in
     372        a regular layout.   (Indexing of these panels in matplotlib is row-
     373        major, i.e. column varies fastest.)
     374
     375        cols == 0 is interpreted as a retangular layout that accomodates
     376        'rows' panels, e.g. rows == 6, cols == 0 is equivalent to
     377        rows == 2, cols == 3.
     378
     379        0 <= n < rows*cols is interpreted as the 0-relative panel number in
     380        the configuration specified by rows and cols to be added to the
     381        current figure as its next 0-relative panel number (i).  This allows
     382        non-regular panel layouts to be constructed via multiple calls.  Any
     383        other value of n clears the plot and produces a rectangular array of
     384        empty panels.
     385        """
     386        if n < 0 and len(self.subplots):
     387            self.figure.clear()
     388            self.set_title()
     389
     390        if rows < 1:
     391            rows = 1
     392
     393        if cols == 0:
     394            i = int(sqrt(rows))
     395            if i*i < rows: i += 1
     396            cols = i
     397
     398            if i*(i-1) >= rows: i -= 1
     399            rows = i
     400
     401        if 0 <= n < rows*cols:
     402            i = len(self.subplots)
     403            self.subplots.append({})
     404            self.subplots[i]['axes']  = self.figure.add_subplot(rows,
     405                                            cols, n+1)
     406            self.subplots[i]['lines'] = []
     407
     408            if i == 0: self.subplot()
     409
     410        else:
     411            self.subplots = []
     412            for i in range(0,rows*cols):
     413                self.subplots.append({})
     414                self.subplots[i]['axes']  = self.figure.add_subplot(rows,
     415                                                cols, i+1)
     416                self.subplots[i]['lines'] = []
     417
     418            self.subplot()
     419
     420
     421    def set_title(self, title=None):
     422        """
     423        Set the title of the plot window.  Use the previous title if title is
     424        omitted.
     425        """
     426        if title is not None:
     427            self.title = title
     428
     429        self.figure.text(0.5, 0.95, self.title, horizontalalignment='center')
    375430
    376431
     
    402457
    403458
    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']
     459    def subplot(self, i=0):
     460        """
     461        Set the subplot to the 0-relative panel number as defined by one or
     462        more invokations of set_panels().
     463        """
     464        l = len(self.subplots)
     465        if l:
     466            i = i%l
     467            self.axes  = self.subplots[i]['axes']
     468            self.lines = self.subplots[i]['lines']
    412469
    413470
Note: See TracChangeset for help on using the changeset viewer.