필터 지우기
필터 지우기

How can I make sure that cbarf (colorbar) will not overlap on second y-axis label?

조회 수: 3 (최근 30일)
% Create a data sets
data = rescale(peaks,0,600);
data1 = rescale(peaks,0,700);
newTickVals = [3 25 300 350 450];
limits=[newTickVals(1) newTickVals(end)];
x=1:1:49;z=ones(size(x));
f1=figure('Position', [0 400 1500 500]);
t1=tiledlayout(1,2);
% Tile 1
nexttile
contourf(data,newTickVals,"ShowText","on")
C=colormap('parula');
colormap(flipud(C))
clim(limits)
colororder({'r'})
yyaxis right
plot(x,z,'red',LineWidth=2)
ylabel('\bf Z values','Color','r','FontSize',18)
% Tile 2
nexttile
contourf(data1,newTickVals,"ShowText","on")
C1=colormap('parula');
colormap(flipud(C1))
h2=cbarf(data1,newTickVals);
Warning: Unable to set 'Position', 'InnerPosition', 'OuterPosition', or 'PositionConstraint' for objects in a TiledChartLayout
cbarf;
clim(limits)
cbarf;
colororder({'r'})
yyaxis right
plot(x,z,'red',LineWidth=2)
ylabel('\bf Z values','Color','r','FontSize',18)
How can I make sure that cbarf (colorbar) will not overlap on second y-axis label?

채택된 답변

Voss
Voss 2023년 7월 18일
편집: Voss 2023년 7월 18일
Like the warning says, you cannot manually set the position of something controlled by a TiledChartLayout.
However, you can avoid using tiledlayout and instead create the axes explicitly and then move them and the colorbar wherever you want.
For example:
% Create a data sets
data = rescale(peaks,0,600);
data1 = rescale(peaks,0,700);
newTickVals = [3 25 300 350 450];
limits=[newTickVals(1) newTickVals(end)];
x=1:1:49;z=ones(size(x));
f1=figure('Position', [0 400 1500 500]);
% Tile 1
axes( ...
'Parent',f1, ...
'Units','normalized', ...
'Position',[0.05 0.05 0.35 0.9]);
contourf(data,newTickVals,"ShowText","on")
C=colormap('parula');
colormap(flipud(C))
clim(limits)
colororder({'r'})
yyaxis right
plot(x,z,'red',LineWidth=2)
ylabel('\bf Z values','Color','r','FontSize',18)
% Tile 2
axes( ...
'Parent',f1, ...
'Units','normalized', ...
'Position',[0.5 0.05 0.38 0.9]) % width after adding cbarf becomes 0.35
contourf(data1,newTickVals,"ShowText","on")
C1=colormap('parula');
colormap(flipud(C1))
h2=cbarf(data1,newTickVals);
set(h2,'Position',[0.9 0.1 0.02 0.8])
clim(limits)
colororder({'r'})
yyaxis right
plot(x,z,'red',LineWidth=2)
ylabel('\bf Z values','Color','r','FontSize',18)
  댓글 수: 2
Ankitkumar Patel
Ankitkumar Patel 2023년 7월 18일
It is very nice solution but I also see that right plot size decrease a bit due to this shifting.
Voss
Voss 2023년 7월 18일
편집: Voss 2023년 7월 18일
You're right. The cbarf makes the right axes a bit narrower. I've changed the answer so that now both axes end up with the same width.

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

추가 답변 (0개)

카테고리

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