How can I create a colormap that maps a certain axes range?

I would like to modify mesh(peaks) and make its colors fall within a particular range. Based on particular Z values I want to change the built-in colormap to the following range.
-2 values and below make grey
greater than -2 up to 4 red
greater than 4 blue

 Accepted Answer

Here is an example that demonstrates how to create a colormap that falls within a particular range.
h = mesh(peaks);
C = get(h,'ZData');
i1 = find(C<=-2);
i2 = find(C>-2 & C<=4);
i3 = find(C>4);
C(i1) = 1 + 0*i1;
C(i2) = 2 + 0*i2;
C(i3) = 3 + 0*i3;
set(h,'CData',C,'CDataMapping','direct');
colormap([.5 .5 .5; 1 0 0; 0 0 1])
The FIND command was used to find the Z values that were in the range that I wanted. Type 'help find' (without the quotes) at the MATLAB command prompt for more information.
The above example uses the Surface property CDataMapping. Here is more information taken from the HelpDesk.
CDataMapping: [ {direct} | scaled ]
Direct or scaled color mapping. This property determines how MATLAB interprets indexed color data used to color the Surface. (If you use true color specification for CData, this property has no effect.)
scaled - transform the color data to span the portion of the colormap indicated by the Axes CLim property, linearly mapping data values to colors. See the caxis reference page for more information on this mapping.
direct - use the color data as indices directly into the colormap. The color data should then be integer values ranging from 1 to length(colormap). MATLAB maps values less than 1 to the first color in the colormap, and values greater than length(colormap) to the last color in the colormap. Values with a decimal portion are fixed to the nearest, lower integer.

More Answers (0)

Categories

Products

Release

R2007a

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!