Hi Brian,

Thanks for the code - this is definitely in the direction of what I want to make!

The RdYlBu_r colormap is one of the built-in colormaps available in matplotlib.pyplot.cm (you can see all of them here:http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps). I think that using the built-in colormaps might give nicer transitions between the colors, so instead of transitioning linearily between red and white and white and blue, it transitions in a slightly non-linear way, along several segments.

Compare:

plot(plt.cm.RdYlBu_r(arange(256)))

with

plot(my_cmap(arange(256)))

I think that the more nonlinear one might look a little bit nicer (and might be less perceptually misleading in interpreting color differences in the result). But I need to figure out how many segments there are in there.

Thanks - Ariel

On Sat, Mar 27, 2010 at 4:14 AM, Brian Blais wrote:
On Mar 27, 2010, at 1:13 , Ariel Rokem wrote:

In particular, I am interested in using the plt.cm.RdYlBu_r colormap. If the data has both negative and positive values, I want 0 to map to the central value of this colormap (a pale whitish yellow) and I want negative values to be in blue and positive numbers to be in red.

not sure if this is what you want (I'd never heard of RdYlBu_r...I need to go read up!), but I've used a similar colormap with the code posted below.  You might be able to modify it for your case.

hope this helps!

bb

from pylab import *

def bluewhitered(a,N=256):
bottom =    [0,   0,  0.5]
botmiddle = [0,  0.5,  1]
middle =    [1,   1,   1]
topmiddle = [1,   0,   0]
top =       [0.5, 0,   0]

lims=[a.min(),a.max()]

if lims[0]<0 and lims[1]>0:
ratio=abs(lims[0])/(abs(lims[0])+lims[1])

cdict={}
cdict['red']=[]
cdict['green']=[]
cdict['blue']=[]

# negative part
red=[(0.0, 0.0, 0.0),
(ratio/2, 0.0, 0.0),
(ratio, 1.0, 1.0)]
green=[(0.0, 0.0, 0.0),
(ratio/2, 0.5, 0.5),
(ratio, 1.0, 1.0)]
blue=[(0.0, 0.5, 0.5),
(ratio/2, 1, 1),
(ratio, 1.0, 1.0)]

cdict['red'].extend(red)
cdict['green'].extend(green)
cdict['blue'].extend(blue)

nratio=1-(1-ratio)/2.0
# positive part
red=[(ratio, 1.0, 1.0),
(nratio, 1.0, 1.0),
(1, 0.5, 0.5)]
green=[(ratio, 1.0, 1.0),
(nratio, 0., 0.),
(1, 0.0, 0.0)]
blue=[(ratio, 1., 1.),
(nratio, 0, 0),
(1, 0, 0)]

cdict['red'].extend(red)
cdict['green'].extend(green)
cdict['blue'].extend(blue)

elif lims[0]>=0:  # all positive
cdict={}
cdict['red']=[]
cdict['green']=[]
cdict['blue']=[]

ratio=0.0
nratio=0.5

# positive part
red=[(ratio, 1.0, 1.0),
(nratio, 1.0, 1.0),
(1, 0.5, 0.5)]
green=[(ratio, 1.0, 1.0),
(nratio, 0., 0.),
(1, 0.0, 0.0)]
blue=[(ratio, 1., 1.),
(nratio, 0, 0),
(1, 0, 0)]

cdict['red'].extend(red)
cdict['green'].extend(green)
cdict['blue'].extend(blue)

else: # all negative
cdict={}
cdict['red']=[]
cdict['green']=[]
cdict['blue']=[]

ratio=1.0

# negative part
red=[(0.0, 0.0, 0.0),
(ratio/2, 0.0, 0.0),
(ratio, 1.0, 1.0)]
green=[(0.0, 0.0, 0.0),
(ratio/2, 0.5, 0.5),
(ratio, 1.0, 1.0)]
blue=[(0.0, 0.5, 0.5),
(ratio/2, 1, 1),
(ratio, 1.0, 1.0)]

cdict['red'].extend(red)
cdict['green'].extend(green)
cdict['blue'].extend(blue)

my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',cdict,N)

return my_cmap

if __name__=="__main__":

a=randn(20,20)
my_cmap=bluewhitered(a,256)

clf()
pcolor(a,cmap=my_cmap)
colorbar()

--
Ariel Rokem
Helen Wills Neuroscience Institute
University of California, Berkeley
http://argentum.ucbso.berkeley.edu/ariel