Color map from green to red

243 次查看(过去 30 天)
Hi all,
I have a map with values and i would like to display the values with colors - from green to red.
The value closest to 0 will be green and the farthest will be red.
Example:
1, 5, 11, 33, 56, 100
1 - Green.
100 - Red.
Can be also:
-5, -23, -43, -55, -80.
-5 - Green.
-80 - Red.
The rest values will be in the order and color bar.

采纳的回答

Akira Agata
Akira Agata 2020-10-17
You can create your original colormap (green to red) and apply to the data.
The following is an example:
% Create green-to-red colormap
cMap = interp1([0;1],[0 1 0; 1 0 0],linspace(0,1,256));
% Apply to the plot
surf(peaks)
colormap(cMap)
colorbar
  4 个评论
Harpreet Singh
Harpreet Singh 2022-4-19
But that is a one way gradient. Didn't Akira want a two way gradient? with gren being in the middle and red going outwards?
DGM
DGM 2022-4-19
编辑:DGM 2022-4-19
I don't really think OP was after an RGB sweep, but more of an HSV hue sweep. Still, if Akira's solution was enough, then it could be made symmetric.
% Create red-green-red colormap
cMap = interp1(0:2,[1 0 0; 0 1 0; 1 0 0],linspace(0,2,256));
% Apply to the plot
surf(peaks)
colormap(cMap)
colorbar
FWIW, for simple RGB primary/secondary sweeps like this, you can improve the linearity to get rid of the dark bands:
% Create red-green-red colormap
cMap = interp1(0:1,[0 1 0; 1 0 0],linspace(0,1,256));
cMap = cMap.^(1/2.4); % linearize
% Apply to the plot
surf(peaks)
colormap(cMap)
colorbar
This gamma adjustment of colormaps can also be done using the misleadingly-named brighten() function, though the parameter behavior is (in my opinion) confusing in the degree to which it obfuscates the otherwise extremely simple math.
Gimme a minute and I'll whip up a colormap that replicates the exact map that OP posted.

请先登录,再进行评论。

更多回答(1 个)

DGM
DGM 2022-4-19
编辑:DGM 2022-4-19
This is how one might go about getting the exact colormap shown in the image and make a symmetric version of it.
A = imread('gyrcb.png');
imshow(A)
% get base color table from image
basect = im2double(permute(A(40,24:517,:),[2 3 1]));
N = 256;
nb = size(basect,1);
% interpolate to get a specified-length version
CT1 = interp1(1:nb,basect,linspace(1,nb,N));
% also make a symmetric version of the same length
CT2 = interp1(1:2*nb,[flipud(basect); basect],linspace(1,2*nb,N));
% Apply the new CT
surf(peaks)
colormap(CT1)
colorbar
% Apply the symmetric CT
figure
surf(peaks)
colormap(CT2)
colorbar
Similar questions:
colorbar extraction (a discrete colormap)
colorbar extraction (includes notes about dealing with compression artifacts)

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by