Matlab custom colormap with only 3 colors

user990639 picture user990639 · Jul 8, 2014 · Viewed 8.5k times · Source

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.

Answer

Luis Mendo picture Luis Mendo · Jul 8, 2014

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.