This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

In a thread on the matplotlib mailling list, James Boyle posted a way to load colormaps from a file. Here it is slightly modified.

gmtColormap.py

   1 def gmtColormap(fileName,GMTPath = None):
   2       import colorsys
   3       import Numeric
   4       N = Numeric
   5       if type(GMTPath) == type(None):
   6           filePath = "/usr/local/cmaps/"+ fileName+".cpt"
   7       else:
   8           filePath = GMTPath+"/"+ fileName +".cpt"
   9       try:
  10           f = open(filePath)
  11       except:
  12           print "file ",filePath, "not found"
  13           return None
  14  
  15       lines = f.readlines()
  16       f.close()
  17  
  18       x = []
  19       r = []
  20       g = []
  21       b = []
  22       colorModel = "RGB"
  23       for l in lines:
  24           ls = l.split()
  25           if l[0] == "#":
  26              if ls[-1] == "HSV":
  27                  colorModel = "HSV"
  28                  continue
  29              else:
  30                  continue
  31           if ls[0] == "B" or ls[0] == "F" or ls[0] == "N":
  32              pass
  33           else:
  34               x.append(float(ls[0]))
  35               r.append(float(ls[1]))
  36               g.append(float(ls[2]))
  37               b.append(float(ls[3]))
  38               xtemp = float(ls[4])
  39               rtemp = float(ls[5])
  40               gtemp = float(ls[6])
  41               btemp = float(ls[7])
  42  
  43       x.append(xtemp)
  44       r.append(rtemp)
  45       g.append(gtemp)
  46       b.append(btemp)
  47  
  48       nTable = len(r)
  49       x = N.array( x , N.Float)
  50       r = N.array( r , N.Float)
  51       g = N.array( g , N.Float)
  52       b = N.array( b , N.Float)
  53       if colorModel == "HSV":
  54          for i in range(r.shape[0]):
  55              rr,gg,bb = colorsys.hsv_to_rgb(r[i]/360.,g[i],b[i])
  56              r[i] = rr ; g[i] = gg ; b[i] = bb
  57       if colorModel == "HSV":
  58          for i in range(r.shape[0]):
  59              rr,gg,bb = colorsys.hsv_to_rgb(r[i]/360.,g[i],b[i])
  60              r[i] = rr ; g[i] = gg ; b[i] = bb
  61       if colorModel == "RGB":
  62           r = r/255.
  63           g = g/255.
  64           b = b/255.
  65       xNorm = (x - x[0])/(x[-1] - x[0])
  66  
  67       red = []
  68       blue = []
  69       green = []
  70       for i in range(len(x)):
  71           red.append([xNorm[i],r[i],r[i]])
  72           green.append([xNorm[i],g[i],g[i]])
  73           blue.append([xNorm[i],b[i],b[i]])
  74       colorDict = {"red":red, "green":green, "blue":blue}
  75       return (colorDict)


CategoryCookbookMatplotlib

SciPy: Cookbook/Matplotlib/Loading_a_colormap_dynamically (last edited 2015-10-24 17:48:24 by anonymous)