Creating and Using Custom Color Maps


How do you create and use your own color maps?


Creating Color Maps

We create our own color map by copying the default color map:

>>> import vcs
>>> v = vcs.init()
>>> my_cm = v.createcolormap('my_cm', 'default')

We customize the color index values by setting each my_cm.index dictionary entry to the desired RGB value. Here is a loop that sets my_cmap to a linear deep-blue to light-blue color map over the range of color indices 16 to 239, inclusive:

for icell in range(16,240):
    rvalue = int( float(icell-16)/(239.-16.) * 90. )
    gvalue = rvalue
    bvalue = 100
    my_cmap.index[icell] = [rvalue, gvalue, bvalue]

Color index 16 is deep-blue, and color index 239 is light-blue. In the next section I'll explain why we only set color indices 16 to 239.

Using Color Maps

To use colors from a color map, the simplest way is to set the color attribute of an object to the desired color index value. For instance, if you have a text-table object my_ttab, the following makes the text black:

my_ttab.color = 241

Many VCS objects have color attributes which you can set this way, including the fillarea, line, marker, text-combined, and text-table objects.

More commonly, color maps are used in drawing filled contour plots (e.g. boxfill and isofill). You could manually set each contour level/range to the colors of your choice, but there's an easier way: create a list of levels with mkevenlevels or mkscale then use getcolors to linearly map color indices over a range to those levels.

Say we have data with a minimum of -3.4 and a maximum of 5.6. Creating a list of levels and a corresponding map of color indices is as simple as:

>>> levels = vcs.mkscale(-3.4, 5.6, 5, zero=1)
>>> print levels
[-4.0, -2.0, 0.0, 2.0, 4.0, 6.0]
>>> print vcs.getcolors(levels)
[16, 127, 128, 184, 239]

The default is for getcolors to scale within the range of color indices of 16 to 239, inclusive. Sometimes it's good to avoid the first few color indices, since elements may default to those values: the default text-table, for instance, has color = 1. Color indices 240 to 255 do not change with different color maps and are not user-changable, so usually you will not scale a color range spanning those values.

The getcolors command works quite well with its default settings, but you might want more control. See this discussion for details.

Finally, once we have a list of color indices that correspond to our levels, can set the respective graphics method attributes accordingly. For instance, if we have an isofill graphics method my_fill_gm, we set my_fill_gm.levels to our list of levels and my_fill_gm.fillareacolors to our list of corresponding color indices. And that's it!

The Python help command (e.g. help(vcs.getcolors)) gives some more information. Descriptions of these VCS function are also found in the Quick Start document (v1.0 has a few typos though).

Notes: This discussion applies to CDAT 3.3.

Return to the Tips and Examples index page.

Updated: December 5, 2003 by Johnny Lin <email address>. License.