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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/797634/image.png)
댓글 수: 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!