Clear Filters
Clear Filters

How to set color scaling?

13 views (last 30 days)
Mr M.
Mr M. on 23 Sep 2016
Edited: Mudambi Srivatsa on 27 Sep 2016
I would like to plot a surface or matrix with colors. How to set a linear scaling automatically by defining z1 to be color1 and z2 to be color2? z1 and z2 are general values not minz or maxz

Answers (1)

Mudambi Srivatsa
Mudambi Srivatsa on 27 Sep 2016
Edited: Mudambi Srivatsa on 27 Sep 2016
I understand that you want to define a color scaling by assigning specific colors to Z values. Further, Z values are general but not min or max values. To do so, you can use 'colors' parameter and direct mapping scheme in "surf" function. For example, the following code snippet builds a color vector that assigns specific colors to the Z values.
[X,Y,Z] = peaks(30);
figure
cmap = [1 0 0; 1 1 0; 0 1 0];
colormap(cmap);
crange = 1;
colors = zeros(size(Z));
colors(Z <= -crange) = 1; % red (1)
colors(Z > -crange & Z < crange) = 2; % yellow (2)
colors(Z >= crange) = 3; % green (3)
surf(X,Y,Z, colors, 'CDataMapping','direct');
I assign the colors based on Z values using a range. In general, you can build your color vector manually or programmatically to assign colors that suit your needs.
Direct mapping uses the color data directly as indices into the colormap. For example, a value of 1 points to the first color in the colormap, a value of 2 points to the second color, and so on. If the color data is noninteger, MATLAB rounds it toward zero. Values greater than the number of colors in the colormap are set equal to the last color in the colormap (i.e., the number length(colormap)). Values less than one are set to one.
For more information on coloring surface plots, refer to the following documentation page:
https://www.mathworks.com/help/matlab/visualize/coloring-mesh-and-surface-plots.html

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!