フィルターのクリア

Custom colorbar with predefined ticks

124 ビュー (過去 30 日間)
Ash Ahamed
Ash Ahamed 2021 年 11 月 12 日
コメント済み: Ash Ahamed 2021 年 11 月 12 日
Hello! I am trying to create a custom colorbar on a countourf plot using the code below.
grid on
colormap(jet(15))
colorbar
c = colorbar
c.FontSize = 12
caxis([0 25.0])
c.Ticks = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 5.0 10.0 15.0 20.0 25.0]
With this code I am getting below colorbar, where as what I wanted is as shown on the right side. I want the colormap on the surface plot to match the colorbar as well. What am I doing wrong here? any advice are greatly appreciated.

採用された回答

DGM
DGM 2021 年 11 月 12 日
編集済み: DGM 2021 年 11 月 12 日
The answer I gave in your last question addressed this issue in part (at least for linear scales). For an arbitrary scale, you can just modify the example
% the given colormap from the last question
cmap = [0.851 0.851 0.851;0.498 1 1;0.298 0.8 1;0 0.6196 1;0 0 0.9529;0.498 1 0.498;0.09804 1 0.09804;0.09804 0.6 0.09804;1 0.749 1;1 0.4 1;0.8 0.09804 1;1 1 0;1 0.651 0;0.9529 0.298 0;0.8 0 0];
% dummy data
x = linspace(0,1,100);
y = x.';
z = max(x,y);
% plot setup
contourf(x,y,z,size(cmap,1)-1)
axis equal
colormap(cmap)
% wrangle the discrete colorbar tick alignment
cb = colorbar;
cbscalein = cb.Limits;
cbscaleout = [min(z(:)) max(z(:))];
ticks = linspace(cbscaleout(1),cbscaleout(2),size(cmap,1)+1);
cb.Ticks = diff(cbscalein)*(ticks-cbscaleout(1))/diff(cbscaleout) + cbscalein(1);
cb.TickLabels = [0:0.1:1 5:5:25]; % this is your arbitrary set of tick values
  1 件のコメント
Ash Ahamed
Ash Ahamed 2021 年 11 月 12 日
Thanks! I remembered that and was trying that out. Thanks for the answer and reminder.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by