0
votes

Using Python and matplotlib, is there a convenient way to do the following:

  1. Establish a mapping between colors and values, say -5 = black, -2 = red, 0 = white, +2 = blue, +5 = black

  2. Generate a colormap that interpolates between those values, similar to matplotlib.colors.LinearSegmentedColormap.from_list()

  3. Make a pseudocolor plot pcolormesh(X,Y,Z), where 0 < Z < 3 using the color coding established above. This means only a subset of the colormap will be used. (The color red will never be used since there are no negative values.)

  4. Add a colorbar ranging from 0 to 3.

This will allow to keep the mapping between values and colors consistent across multiple plots.

1
I think you can do this with LinearSegmentedColormap and proper limits on your normalization function. Color maps take an argument in [0, 1], so your just need to map your values onto that range as you desire.tacaswell
This is a duplicate of stackoverflow.com/questions/18926031/…. The answer there is quite good.MattZ

1 Answers

0
votes

You need to make a custom colormap for a LinearSegmentedColormap. The explanation for how the colormap dictionary below can be understood is given in the LinearSegmentedColormap docs:

# Create the desired color dictionary
cdict = { 'red'   : ((0.0, 0.0, 0.0),
                     (0.3, 1.0, 1.0), 
                     (0.5, 1.0, 1.0),
                     (0.7, 0.0, 0.0),
                     (1.0, 0.0, 0.0)),
          'green' : ((0.0, 0.0, 0.0),
                     (0.3, 0.0, 0.0),
                     (0.5, 1.0, 1.0),
                     (0.7, 0.0, 0.0),
                     (1.0, 0.0, 0.0)),
          'blue'  : ((0.0, 0.0, 0.0),
                     (0.3, 0.0, 0.0),
                     (0.5, 1.0, 1.0),
                     (0.7, 1.0, 1.0),
                     (1.0, 0.0, 0.0)),
        }

# Create a colormap from the above dictionary
cmap = LinearSegmentedColormap('CustomColormap', cdict, 1024)
# Use the colormap in your plot
contour = pcolormesh(X,Y,Z,cmap=cmap)
# Supply the contour to the colorbar
colorbar(contour)

Basically, the colormapping can only exist from 0-1, so for color changes to happen at -5, -2, 0, 2 and 5 you would need the points to be at 0 (-5), 0.3 (-2), 0.5 (0), 0.7 (2), and 1 (5).

To get the effect you want, you will need to make sure you plot over the range -5 to 5, but then set the colorbar to only show from 0 to 3.