Color map from green to red

274 Ansichten (letzte 30 Tage)
Idan Hamawi
Idan Hamawi am 17 Okt. 2020
Bearbeitet: DGM am 19 Apr. 2022
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.

Akzeptierte Antwort

Akira Agata
Akira Agata am 17 Okt. 2020
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 Kommentare
Harpreet Singh
Harpreet Singh am 19 Apr. 2022
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 am 19 Apr. 2022
Bearbeitet: DGM am 19 Apr. 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

DGM
DGM am 19 Apr. 2022
Bearbeitet: DGM am 19 Apr. 2022
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)

Community Treasure Hunt

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

Start Hunting!

Translated by