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

5 次查看(过去 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 个评论
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