Custom colorbar with predefined ticks

148 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Colormaps 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by