MATLAB Answers


Assign only 2 colors in a surf plot, one color for values smaller 27 and one for all bigger

Asked by Julien
on 19 May 2013

Hello, if somebody could help, it would really great. I want to have a plot using the surf command with one color for values below 27 and one other for values above. Is it possible to do it easily ? I have checked the options on colormap but I dont know what to change.

At this stage the "surf" is perfectly working with values for z going from 3.6 to 2431 and therefore with colors going from blue to red.

Best regards and thank you for the answer.


 x = reshape(untitled(:,1),12,9)';
 y = reshape(untitled(:,2),12,9)';
 z = reshape(untitled(:,3),12,9)';
 %z =20*log10(reshape(untitled(:,3),12,9)');

figure(1) surf(x,y,z)



No products are associated with this question.

2 Answers

Answer by Image Analyst
on 20 May 2013
 Accepted answer

Try this, where I make it red above 27 and cyan below 27:

% Generate sample data in the rand 1.3 to 45.3
z = 3*(peaks(100)+7);
% Make colors be red above 27, and cyan below 27.
redChannel = z > 27;
greenChannel = z < 27;
blueChannel = greenChannel;
% Make the RGB image.
colors = double(cat(3, redChannel, greenChannel, blueChannel));
% Plot the surface with those colors.
surf(z, colors);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

  1 Comment

on 20 May 2013

Hello, great, thank you for your answer! It is what I was searching for. Best regards.

Answer by Arthur Allen on 20 May 2013

Answer #1, works well for simple colors divisions, and can be expanded to additional bands with 'ifelse' statements. However, if you are trying to use colormap and colormapeditor the Matlab Help skips a really important detail. That is you have to decide up front, the number of colors in colormap that is right for your display. In your example this would be two, setting

>> Julian_colormap = colormap(jet(2)); >> colormapeditor

Once in ‘Colormap Editor’ you would set Color data min: 0.0 Color data max: 54.0

Following the example in Colormapeditor, click on bars to change color to suit, (red and green in your case), and also click on bars to see Index 1 starts a 0E0 and Index 2 starts at 27.0

Now, if you want 10 colors, use:

>> Julian10_colormap = colormap(jet(10));

And use colormapeditor, follow colormapeditor HELP example to modify, and ‘save’ and ‘load’ your custom Julian10_colormap for later use.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!