Matlab custom colormap with only 3 colors

蓝咒 提交于 2019-12-01 13:37:00

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

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.

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

Dan

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

Ander Biguri

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!