# creating colormaps

Thanks Eric,

That is much better. I am going to try and implement it now and see how I go. I will let you know.

Regards, Marjolaine.

Eric Firing <efiring@...202...> 07/09/08 11:12 PM >>>

Marjolaine Rouault wrote:

Hi,

I don't understand how one creates his own colormap. i am using
matplotlib and I have checked the example in
http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps but I can't
really understand how it works. Are there any other examples out
there? I want to create a colormap a bit like the one at this url:

http://www.pyngl.ucar.edu/Graphics/Images/ViBlGrWhYeOrRe.gif

best regards, Marjolaine.

Marjolaine,

colormap--you can use either a LinearSegmentedColormap or a
ListedColormap. If you already have a colormap in the form of a list of
evenly-spaced colors, then the simplest way to get it into mpl is by
using that list to initialize a ListedColormap. If, instead, you have a
general idea of how you want R, G, and B to vary over the range of the
map, then you probably need the LinearSegmentedColormap.

Every description of the LinearSegmentedColormap class that I have seen
is confusing, even though the way it works is fairly simple. Let's see
if I can make it a little less confusing.

Example: suppose you want red to increase from 0 to 1 over the bottom
half, green to do the same over the middle half, and blue over the top
half. Then you would use:

cdict = { 'red': ((0, 0, 0),
(0.5, 1, 1),
(1, 1, 1)),
'green': ((0, 0, 0),
(0.25, 0, 0),
(0.75, 1, 1),
(1, 1, 1)),
'blue': ((0, 0, 0),
(0.5, 0, 0),
(1, 1, 1))}

If, as in this example, there are no discontinuities in the r, g, and b
components, then it is quite simple: the second and third element of
each tuple, above, is the same--call it "y". The first element ("x")
defines interpolation intervals over the full range of 0 to 1, and it
must span that whole range. In other words, the values of x divide the
0-to-1 range into a set of segments, and y gives the end-point color
values for each segment.

Now consider the green. cdict['green'] is saying that for
0 <= x <= 0.25, y is zero; no green.
0.25 < x <= 0.75, y varies linearly from 0 to 1.
x > 0.75, y remains at 1, full green.

If there are discontinuities, then it is a little more complicated.
Label the 3 elements in each row in the cdict entry for a given color as
(x, y0, y1). Then for values of x between x[i] and x[i+1] the color
value is interpolated between y1[i] and y0[i+1].

Going back to the cookbook example, look at cdict['red']; because y0 !=
y1, it is saying that for x from 0 to 0.5, red increases from 0 to 1,
but then it jumps down, so that for x from 0.5 to 1, red increases from
0.7 to 1. Green ramps from 0 to 1 as x goes from 0 to 0.5, then jumps
back to 0, and ramps back to 1 as x goes from 0.5 to 1.

row i: x y0 y1
/
/
row i+1: x y0 y1

Above is an attempt to show that for x in the range x[i] to x[i+1], the
interpolation is between y1[i] and y0[i+1]. So, y0[0] and y1[-1] are
never used.

I hope I got all that right--I would welcome close checking. I want to
get an adequate and correct explanation into the standard mpl documentation.

Eric

···

--
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean. MailScanner thanks Transtec Computers for their support.