Ignore:
Timestamp:
06/22/05 15:54:38 (19 years ago)
Author:
mar637
Message:

removed color loading as mpl now supports named colors. some minor corrections on pol label handling. Also added orientation option for ps output.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asaplot.py

    r620 r652  
    2121matplotlib.rcParams['toolbar'] = 'toolbar2'
    2222
    23 # Colour dictionary.
    24 colours = {}
    25 
    2623class ASAPlot:
    2724    """
     
    6360
    6461        # Set matplotlib default colour sequence.
    65         self.colours = [1, 'b', 'g', 'r', 'c', 'm', 'y', 'k']
     62        self.colormap = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black', 'purple', 'orange', 'pink']
     63        self.color = 0;
    6664        self.attributes = {}
    6765        self.loc = 0
     
    7876        """
    7977
    80         for i in range(1,len(self.lines)+1):
     78        for i in range(len(self.lines)):
    8179           self.delete(i)
    8280        self.axes.clear()
    83         self.colours[0] = 1
     81        self.color = 0
    8482        self.lines = []
    8583
     84
     85    def palette(self, color, colormap=None):
     86        if colormap:
     87            self.colormap = colormap
     88        if 0 <= color < len(self.colormap):
     89            self.color = color
    8690
    8791    def delete(self, numbers=None):
     
    179183
    180184
    181     def palette(self, pen=None, colours=None):
    182         """
    183         Redefine the colour sequence.
    184 
    185         pen is the pen number to use for the next plot; this will be auto-
    186         incremented.
    187 
    188         colours is the list of pen colours.  Colour may be specified via
    189         the single letter values understood by matplotlib:
    190 
    191             b: blue
    192             g: green
    193             r: red
    194             c: cyan
    195             m: magenta
    196             y: yellow
    197             k: black
    198             w: white
    199 
    200         or via the full name as listed in the colour dictionary which is
    201         loaded by default by load_colours() from rgb.txt and listed by
    202         list_colours().
    203         """
    204 
    205         if pen is None and colours is None:
    206             self.colours = []
    207             return
    208 
    209         if pen is None:
    210             if not len(self.colours):
    211                 self.colours = [1]
    212         else:
    213             self.colours[0] = pen
    214 
    215         if colours is None:
    216             return
    217 
    218         cols = []
    219         for col in colours:
    220             cols.append(get_colour(col))
    221 
    222         self.colours[1:] = cols
    223 
    224         if 0 > self.colours[0] > len(self.colours):
    225             self.colours[0] = 1
    226 
    227185
    228186    def plot(self, x=None, y=None, mask=None, fmt=None, add=None):
     
    283241                getattr(segment, "set_%s"%k)(v)
    284242
    285         if not gotcolour and len(self.colours):
     243        if not gotcolour and len(self.colormap):
    286244            for segment in self.lines[i]:
    287                 getattr(segment, "set_color")(self.colours[self.colours[0]])
    288 
    289             self.colours[0] += 1
    290             if self.colours[0] >= len(self.colours):
    291                 self.colours[0] = 1
     245                getattr(segment, "set_color")(self.colormap[self.color])
     246
     247            self.color += 1
     248            if self.color >= len(self.colormap):
     249                self.color = 0
    292250
    293251        self.show()
     
    464422
    465423        newargs = {}
     424       
    466425        for k, v in kwargs.iteritems():
    467426            k = k.lower()
    468427            if k == 'colour': k = 'color'
    469 
    470             if k == 'color':
    471                 v = get_colour(v)
    472 
    473428            newargs[k] = v
    474429
     
    486441        if what is None: return
    487442        if what[-6:] == 'colour': what = what[:-6] + 'color'
    488         if what[-5:] == 'color' and len(args):
    489             args = (get_colour(args[0]),)
     443        #if what[-5:] == 'color' and len(args):
     444        #    args = (get_colour(args[0]),)
    490445
    491446        newargs = {}
     
    493448            k = k.lower()
    494449            if k == 'colour': k = 'color'
    495 
    496             if k == 'color':
    497                 v = get_colour(v)
    498 
    499450            newargs[k] = v
    500451
     
    556507            if k == 'colour': k = 'color'
    557508
    558             if k == 'color':
    559                 v = get_colour(v)
    560 
    561509            if 0 <= number < len(self.lines):
    562510                if self.lines[number] is not None:
     
    743691        """
    744692        self.window.wm_withdraw()
    745 
    746 
    747 def get_colour(colour='black'):
    748     """
    749     Look up a colour by name in the colour dictionary.  Matches are
    750     case-insensitive, insensitive to blanks, and 'gray' matches 'grey'.
    751     """
    752 
    753     if colour is None: return None
    754 
    755     if match('[rgbcmykw]$', colour): return colour
    756     if match('#[\da-fA-F]{6}$', colour): return colour
    757 
    758     if len(colours) == 0: load_colours()
    759 
    760     # Try a quick match.
    761     if colours.has_key(colour): return colours[colour]
    762 
    763     colour = colour.replace(' ','').lower()
    764     colour = colour.replace('gray','grey')
    765     for name in colours.keys():
    766         if name.lower() == colour:
    767             return colours[name]
    768 
    769     return '#000000'
    770 
    771 
    772 def list_colours():
    773     """
    774     List the contents of the colour dictionary sorted by name.
    775     """
    776 
    777     if len(colours) == 0: load_colours()
    778 
    779     names = colours.keys()
    780     names.sort()
    781     for name in names:
    782         print colours[name], name
    783 
    784 
    785 def load_colours(filename='/usr/local/lib/rgb.txt'):
    786     """
    787     Load the colour dictionary from the specified file.
    788     """
    789     print 'Loading colour dictionary from', filename
    790     from os.path import expandvars
    791     filename = expandvars(filename)
    792     rgb = open(filename, 'r')
    793 
    794     while True:
    795         line = rgb.readline()
    796         if line == '': break
    797         tmp = line.split()
    798 
    799         if len(tmp) == 4:
    800             if tmp[3][:4] == 'gray': continue
    801             if tmp[3].lower().find('gray') != -1: continue
    802 
    803             name = tmp[3][0].upper() + tmp[3][1:]
    804             r, g, b = int(tmp[0]), int(tmp[1]), int(tmp[2])
    805             colours[name] = '#%2.2x%2.2x%2.2x' % (r, g, b)
Note: See TracChangeset for help on using the changeset viewer.