From: Greg N. <no...@uc...> - 2005-03-23 17:49:49
|
* James Boyle <bo...@ll...> wrote: > I would like to be able to define a new colormap dynamically, that > is without altering code in cm.py. It would not appear to be too > difficult to some one who knew what they were doing. I have made > some attempts following the code in cm.py and other places, but I do > not seem to be able to get the normalization correct. Do you mean that you're having trouble with the LinearSegmentedColormap function? I've had to do the same thing, and I've included below my code to generate two colormaps, one is the same as "hot" but reversed, and the other fades from read to blue. The form seems to be: data = {'red': ((0,r0,r0), (i1, r1, r1), (i2, r2, r2), ... (1, rn, rn)), 'green': ... similar, 'blue': ... similar} colormap = pylab.cm.colors.LinearSegmentedColormap('colormap', data, pylab.rcParams['image.lut']) where i1, i2... are independent variable values from zero to 1, and r1, r2... are red values from zero to one associated with that variable value. Each red value appears twice to allow for discontinuous functions, but I haen't had to use that. > I know that I could just put the data into cm.py but I would rather > not have to remember to alter this code for every release of > matplotlib and I am sure most people do not want my colormaps. One of the things I like about python is that you can stuff things into other namespaces without altering the original code. This is probably not recommended and will break things, etc, but I use it to add functionality that I consider to be missing from the software I use. This allows me to forget whether the function is my own extension to the package or a "native" function in the package. Ie, I'm sure that you don't want to be typing things like: subplot(211); pcolor(data_1,cmap=cm.hot) subplot(212); pcolor(data_2,cmap=mycoolcolormaps.map) because then you'll have to remember which colormaps are in which place. So you could do something similar to this: # Add some stuff to the scipy namespace def lrange(l, h, dex): """Log-spaced array""" return 10**scipy.arange(scipy.log10(l), scipy.log10(h), dex) scipy.lrange = lrange > I have used GMT (Generic Mapping Tools) extensively in the past and > have code to translate the GMT color map into a form usable by > matplotlib. I don't have anything to offer, but I'd like more colormaps, too, so I'm interested in whatever comes out of this discussion... Greg |