views:

42

answers:

1

I am using pcolor with a custom color map to plot a matrix of values. I set my color map so that low values are white and high values are red, as shown below. All of my matrices have values between 0 and 20 (inclusive) and I'd like 20 to always be pure red and 0 to always be pure white, even if the matrix has values that don't span the entire range. For example, if my matrix only has values between 2 and 7, I don't want it to plot 2 as white and 7 as red, but rather color it as if the range is still 0 to 20. How can I do this? I tried using the "ticks=" option of colorbar but it did not work.

Here is my current code (assume "my_matrix" contains the values to be plotted):

cdict = {'red': ((0.0, 1.0, 1.0),
         (0.5, 1.0, 1.0),
         (1.0, 1.0, 1.0)),
     'green': ((0.0, 1.0, 1.0),
           (0.5, 1.0, 1.0),
           (1.0, 0.0, 0.0)),
     'blue': ((0.0, 1.0, 1.0),
          (0.5, 1.0, 1.0),
          (1.0, 0.0, 0.0))}
my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', cdict, 256)
colored_matrix = plt.pcolor(my_matrix, cmap=my_cmap)
plt.colorbar(colored_matrix, ticks=[0, 5, 10, 15, 20])

any idea how I can fix this to get the right result? thanks very much.

+1  A: 

A guess: Your colormap is probably fine. Try to adjust the vmin and vmax when plotting.

pylab.imshow(im, vmin=0, vmax=20)
Steve