I have a map of different colors, I need all z values from greater than 0 to be red, all zero values to be white, and all below zero values to be blue

11 ビュー (過去 30 日間)
color specifications
imagesc(z)
cmap = [1 0 0 ; 0 1 0 ; 0 0 1] ;
colorbar
colormap(cmap)
I applied the code above, but it just seperated the ranges equaly.

採用された回答

Walter Roberson
Walter Roberson 2020 年 2 月 12 日
Zz = (z>0) - (z<0);
imagessc(Zz)
colormap(cmap)
caxis([-1,1])
The caxis is there for protection in case z does not have a mix of negative and positive values.
If there can be nan in z then
Zz(isnan(z)) = nan;
  2 件のコメント
Megan Mirkhanian
Megan Mirkhanian 2020 年 2 月 13 日
Is there a way to label the colors as high, none and low?
Walter Roberson
Walter Roberson 2020 年 2 月 13 日
h = colorbar;
h.Ticks = [-1 0 1];
h.TickLabels = {'low', 'none', 'high'};

サインインしてコメントする。

その他の回答 (1 件)

Shivaraj Durairaj
Shivaraj Durairaj 2020 年 2 月 12 日
You can specify the clims argument to the imagesc function.
The values in z that are less than or equal to cmin (-1) map to the first color in the colormap. Values greater than or equal to cmax (1) map to the last color in the colormap. Values between cmin and cmax linearly map to the colormap.
For example,
z = randi([-10,10],[4,4]);
imagesc(z,[-1,1]) % clims = [cmin cmax]
cmap = [0 0 1;1 1 1;1 0 0]; %[blue;white;red]
colormap(cmap)
colorbar
Note: The above implementation works well when elements of z are integers. In case of non-integers, you may consider to limit the values of the elements to -1,0,1 after initializing z.
z(z<0) = -1;
z(z>0) = 1;
z(z==0) = 0;

カテゴリ

Help Center および File ExchangeOrange についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by