Eugene
Eugene

Reputation: 1125

Matlab custom colormap with only 3 colors

just want to check if it is possible to make a custom colormap with only 3 colors? (there is no need for gradient).

Example: Data ranges from 0-100,

Upvotes: 1

Views: 9728

Answers (4)

Luis Mendo
Luis Mendo

Reputation: 112659

Just use a colormap with three rows. Each row defines a color in terms of R, G, B components.

A = randi(100,16,16); %// example data
imagesc(A) %// display matrix as image
colormap([1 0 0; 0 1 0; 0 0 1]) %// apply colormap
colorbar %// show color bar

enter image description here

This defines uniformly spaced thresholds between colors. If you need more control you need to have more than three rows, with some of the colors repeated. For example,

colormap([1 0 0; 1 0 0; 0 1 0; 0 0 1]) %// apply colormap

will define a 50% threshold for first color, 75% for second and 100% for third.

Upvotes: 7

Amro
Amro

Reputation: 124543

Take this example:

% some matrix with integer values in the range [0,100]
Z = peaks;
Z(:) = round((Z(:)-min(Z(:))) ./ range(Z(:))*100);

% show as image (with scaled color mapping)
image(Z, 'CDataMapping','scaled')
caxis([0 100])    % set axes CLim property
colormap(eye(3))  % set figure Colormap property
colorbar          % show colorbar

Note that the colors are scaled to the range [0 100], that range is mapped to the current figure's colormap (which we set to only three colors).

peaks

Upvotes: 3

Ander Biguri
Ander Biguri

Reputation: 35525

Check my answer here

You can use that code and decide to interpolate between values or not, its just 2 lines of the code.

The result image shown in the original post for a GYR cutom colormap.

enter image description here

Upvotes: 1

Dan
Dan

Reputation: 45741

Follow this example: How to create a custom colormap programmatically? but instead of R = linspace(0,t(1),50)' you would use R = ones(50,1)*t(1)

or even simpler:

if colour 1 is t1 = [r1, g1, b1] etc then

map(1:34, :) = repmat(t1, 33, 1)
map(35:68, :) = repmat(t2, (67-34), 1)

etc...

OR

map(1:34, :) = bsxfun(@times, t, ones(33,3)) etc...

Upvotes: 1

Related Questions