1

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,

  • so 0-33 is one color,
  • 34-67 is another color,
  • and 68-100 is another color.
Amro
  • 123,847
  • 25
  • 243
  • 454
Eugene
  • 1,013
  • 1
  • 22
  • 43

4 Answers4

7

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.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
3

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

Amro
  • 123,847
  • 25
  • 243
  • 454
1

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...

Community
  • 1
  • 1
Dan
  • 45,079
  • 17
  • 88
  • 157
1

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

Community
  • 1
  • 1
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120