Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
% 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?

Akzeptierte Antwort

Voss
Voss am 18 Jul. 2023
Bearbeitet: Voss am 18 Jul. 2023
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 Kommentare
Ankitkumar Patel
Ankitkumar Patel am 18 Jul. 2023
It is very nice solution but I also see that right plot size decrease a bit due to this shifting.
Voss
Voss am 18 Jul. 2023
Bearbeitet: Voss am 18 Jul. 2023
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by