Color map from green to red

236 ビュー (過去 30 日間)
Idan Hamawi
Idan Hamawi 2020 年 10 月 17 日
編集済み: DGM 2022 年 4 月 19 日
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 Center および File ExchangeBlue についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by