フィルターのクリア

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

3 ビュー (過去 30 日間)
Ankitkumar Patel
Ankitkumar Patel 2023 年 7 月 17 日
編集済み: Voss 2023 年 7 月 18 日
% 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 Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by