![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/383358/image.png)
Color map from green to red
186 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/383343/image.png)
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.
0 commentaires
Réponse acceptée
Akira Agata
le 17 Oct 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/383358/image.png)
4 commentaires
Harpreet Singh
le 19 Avr 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
le 19 Avr 2022
Modifié(e) : DGM
le 19 Avr 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.
Plus de réponses (1)
DGM
le 19 Avr 2022
Modifié(e) : DGM
le 19 Avr 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)
0 commentaires
Voir également
Catégories
En savoir plus sur Blue dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!