how to set a fixed color bar for pcolor in python matplotlib?

Posted by user248237 on Stack Overflow See other posts from Stack Overflow or by user248237
Published on 2010-06-14T00:49:53Z Indexed on 2010/06/14 0:52 UTC
Read the original article Hit count: 223

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy