help required using colormap function in matlab

3 views (last 30 days)
Hello Everyone,
I am using surf function to plot a 2D matrix Z. The matrix has 61 rows and 98 columns. The data varies from 26.5 to 33.5 , and in few places I have some zeros. Till now i was using the default colourmap for plotting my data using surf command. But now I want more control over the colour pattern. I was reading through the documentation of colormap. It says that
"Specify an array of color data that is equal in size to the z- data and is used for indexed colors"
I create a matrix C of same size as Z but I still did not understand what values to put so that I can use the surf function as
figure(),surf(Z,C)

Accepted Answer

Daniel Shub
Daniel Shub on 22 Jul 2013
It is not clear what you are trying to do. In the simplest case you might be trying to do
Z = 7*rand(61,98)+26.5;
C = ndgrid(1:size(Z,1), 1:size(Z,2));
but I suspect you are trying to change the colormap
set(gcf, 'ColorMap', [1:64; 64:-1:1; [32:-1:1, 64:-1:33]]'/64)
colorbar
  4 Comments
Ricky
Ricky on 22 Jul 2013
Thanks Daniel for your reply can you please also tell the reason for choosing a 64*3 matrix. The default colormap also uses a 64*3 matrix. I created a color matrix C of size 61*98 what values should I fill in it. Thanks for helping
Daniel Shub
Daniel Shub on 23 Jul 2013
A 61x98 matrix is not a color map. A color map must be Nx3 where the three values in each row represents a RGB values. I chose N to be 64 since 64 different colors tends to be enough to be percieved as visually continuous. If you have discrete data with only 8 values, then you could set N to be 8.
In surf(Z) MATLAB plots a 3D surface with the height of the surface defined by Z. It then uses the color map to assign color such that smaller Z have values from the "bottom" of the color map and larger Z have values from the "top" of the map. What those colors are depends on the color map.
In surf(Z,C) Z is still the height, but MATLAB choses the color from the color map based on C.

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 22 Jul 2013
You can modify it interactively by using the colormapeditor
surf(peaks);
colormapeditor
(Also available through: Edit Menu -> Colormap)

Community Treasure Hunt

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

Start Hunting!