필터 지우기
필터 지우기

How to match colorbar and countourf plot manually?

조회 수: 4 (최근 30일)
Ankitkumar Patel
Ankitkumar Patel 2023년 7월 6일
댓글: DGM 2023년 7월 7일
clear all;
close all;
x=1:10;x=double(x)
x = 1×10
1 2 3 4 5 6 7 8 9 10
y=x';y=double(y)
y = 10×1
1 2 3 4 5 6 7 8 9 10
z=repmat(x,10,1)
z = 10×10
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
maxColorLimit=12
maxColorLimit = 12
minColorLimit=0
minColorLimit = 0
l=maxColorLimit:-1:minColorLimit
l = 1×13
12 11 10 9 8 7 6 5 4 3 2 1 0
npoints=floor(length(l))
npoints = 13
levels=linspace(maxColorLimit,minColorLimit,npoints)
levels = 1×13
12 11 10 9 8 7 6 5 4 3 2 1 0
s1=contourf(x,y,z,levels)
s1 = 2×99
2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 10 10 9 8 7 6 5 4 3 2 1 10 10 9 8 7 6 5 4 3 2 1 10 10 9 8 7 6 5 4
c1=colorbar
c1 =
ColorBar with properties: Location: 'eastoutside' Limits: [1 10] FontSize: 9 Position: [0.8302 0.1109 0.0381 0.8152] Units: 'normalized' Show all properties
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?

채택된 답변

DGM
DGM 2023년 7월 6일
Set your caxis()/clim() values. Set your colormap length according to the number of discrete levels you actually have.
x = 1:10;
y = x';
z = repmat(x,10,1);
% this only sets the mapping and colorbar
colorbarrange = [0 12]; % assumed to be integer values
levels = colorbarrange(1):colorbarrange(2);
maplength = diff(colorbarrange);
% plot things
s1 = contourf(x,y,z,levels);
c1 = colorbar;
colormap(parula(maplength))
caxis(colorbarrange)
% if you really want to make sure every tick is labeled
xticks(gca,ceil(min(x(:))):floor(max(x(:))));
c1.Ticks = levels;
  댓글 수: 3
Ankitkumar Patel
Ankitkumar Patel 2023년 7월 7일
I also asked one more question: Kind of next step of this one
Check it here: https://de.mathworks.com/matlabcentral/answers/1992888-only-one-colorbar-for-subplots-while-plotting-in-a-for-loop
DGM
DGM 2023년 7월 7일
I write around caxis, since I still run R2019b; clim() wasn't an option until R2022a.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Angelo Yeo
Angelo Yeo 2023년 7월 6일
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
Ankitkumar Patel 2023년 7월 6일
편집: Ankitkumar Patel 2023년 7월 6일
Thank you for your answer but want to do it opposite of it. I want colorbar as shown from 1 to 12 in which colorscale for 1 to 10 should match with colorplot.
Angelo Yeo
Angelo Yeo 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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by