How to match colorbar and countourf plot manually?
이전 댓글 표시
clear all;
close all;
x=1:10;x=double(x)
y=x';y=double(y)
z=repmat(x,10,1)
maxColorLimit=12
minColorLimit=0
l=maxColorLimit:-1:minColorLimit
npoints=floor(length(l))
levels=linspace(maxColorLimit,minColorLimit,npoints)
s1=contourf(x,y,z,levels)
c1=colorbar
colormap(c1,'Parula(11)')
c1.Ticks=flip(maxColorLimit:-1:minColorLimit);
I want color barto match exactly with the countour plot. I want colorbar and plot to look like below. In short 0 to 1, 10 to 12 colorbar colors also should be out of it. What am i doing wring here?


채택된 답변
추가 답변 (1개)
The contourf shows the isolevel of the height z and tries to fill the gaps between contour lines. Same color means the same height. Please take a look at the following visualization.
x=1:10;
y=x';
z=repmat(x,10,1);
ax = gca;
[M, c] = contourf(x,y,z, "ShowText", true);
c1=colorbar;
colormap(parula)
As you can see the rightmost color strip tries to fill the place where the value is assumed to be "9". In order to get what you want, I think you can use imagesc.
imagesc(x, y, z);
c1=colorbar;
colormap(parula)
Note how x and y ticks are different from each other.
댓글 수: 2
Ankitkumar Patel
2023년 7월 6일
편집: Ankitkumar Patel
2023년 7월 6일
Okay. I explained why you get unexpected behavior. Then you can try this to work around your issue. But note that this is not a robut solution for your problem.
x=1:10;
y=x';
z=repmat(x,10,1);
ax = gca;
[M, c] = contourf(x,y,z);
c1=colorbar;
colormap(c1,'Parula(11)')
c1.Ticks = linspace(1, 10, 12);
c1.TickLabels = string(1:12);
카테고리
도움말 센터 및 File Exchange에서 Color and Styling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




