Custom colorbar with predefined ticks
124 次查看(过去 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.
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!