How can I create non-uniform color divisions in a Colorbar ?

23 ビュー (過去 30 日間)
Varun Pai
Varun Pai 2022 年 9 月 21 日
コメント済み: Robert U 2022 年 9 月 21 日
I have a matrix of N x 3 dimension. The 1st and 2nd column are used to plot X and Y axis respectively. The 3rd column value (range 0~1) is plotted using color in the axis. I use the following code
A = rand(10,3);
figure
colormap(jet(3))
scatter(A(:,1), A(:,2), 30, A(:,3), 'filled');
colorbar
caxis([0 1])
The code resulted in above plot. Here, since I mentioned jet(3), the colorbar is divided equally into three.
In my case, I need to set 2 threshold values t1 and t2 so that the colorbar gets divided at this threshhold.
For e.g. I set t1=0.1 and t2=0.4, the colorbar should be divided as follows
0-0.1 : Blue , 0.1-0.4 : Cyan , 0.4-1: Yellow
Is there any method to split colorbar non-uniformly ? Any leads will be appreciated.

採用された回答

Robert U
Robert U 2022 年 9 月 21 日
Hi Varun Pai,
you have to create your own colormap in order to have non-uniformly distributed colors. Due to your division of the color axis you have to use 10 color definitions.
A = rand(10,3);
fh = figure;
ah = axes(fh);
cm = colormap(jet(3));
% 0 : 0.1 - dark blue
% 0.1 : 0.4 - light blue
% 0.4 : 1.0 - yellow
% use ten color containers
newColormap = cm(1,:);
newColormap = [newColormap; repmat(cm(2,:),3,1)];
newColormap = [newColormap; repmat(cm(3,:),6,1)];
scatter(ah, A(:,1), A(:,2), 30, A(:,3), 'filled');
cb = colorbar(ah);
caxis(ah,[0 1])
colormap(ah,newColormap);
Kind regards,
Robert
  2 件のコメント
Varun Pai
Varun Pai 2022 年 9 月 21 日
Thank you Robert. I understand your solution.
In some case, I have threshold values up to 4 places of decimals. for e.g: 0.1673. So I think I have to divide the whole colorbar based on number of decimal places and then use repmat for same color.
Am I right ?
It would have been nice if matlab allows to set custom ranges.
Robert U
Robert U 2022 年 9 月 21 日
Unfortunately, there is no way of defining intermediate color limits. Maybe you reconsider whether or not visual tricks will help you to overcome the need for accuracy. Otherwise, yes, you would have to define your colormap in the granularity of what you desire to resolve.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by