How to add a shared ylabel on the right hand-side of a figure, when combining tiledlayout and yyaxis?

In addition to a shared ylabel on the left hand-side of a figure, I want to put a shared ylabel on the right hand-side of the tiledlayout, when also using yyaxis. How can I achieve this? I have prepared the following MWE:
figure
t = tiledlayout(4,1);
for i = 1:4
nexttile
title("Tile"+i)
yyaxis left
plot(rand(1,10))
yyaxis right
plot(rand(1,10))
end
title(t,"Tiles")
xlabel(t,"x")
ylabel(t,"y left")
% ylabel(t,"y right")
Thanks in advance!
Edit:
My yticklabels on the left as well as on the right hand-side are relatively long, therefore, both shared ylabels need to factor the necessary distance in. The shared ylabel on the left hand-side does this automatically, the one on the right hand-side should do the same. I have updated the MWE (unfortunately, it now acts a bit buggy but it should illustrate the point, nonetheless):
figure
t = tiledlayout(4,1);
longYTickLabels_left = ["negative yticklabel left" "0" "positive yticklabel left" ];%edit: added
longYTickLabels_right = ["negative yticklabel right" "0" "positive yticklabel right"];%edit: added
for i = 1:4
nexttile
title("Tile"+i)
yyaxis left
plot(rand(1,10))
yticklabels(longYTickLabels_left)%edit: added
yyaxis right
plot(rand(1,10))
yticklabels(longYTickLabels_right)%edit: added
end
title(t,"Tiles")
xlabel(t,"x")
ylabel(t,"y left")
% ylabel(t,"y right")

Answers (1)

One way.
t = tiledlayout(4,1);
for i = 1:4
nexttile
title("Tile"+i)
yyaxis left
plot(rand(1,10))
yyaxis right
plot(rand(1,10))
end
title(t,"Tiles")
xlabel(t,"x")
ylabel(t,"y left")
% Create invisible axes
axBig = axes(t.Parent);
axBig.Position = t.Position.*[1,1,1.05,1];
axBig.Color = 'none';
axBig.XTick = [];
axBig.YTick = [];
axBig.XColor='none';
axBig.YColor='none';
% Put ylabel on the right
axBig.YAxisLocation = 'right';
ylabel(axBig,'y right','Color','k');

4 Comments

I've seen it, but I don't know why you think it should change my answer.
When I tested your solution, the yticklabels and the ylabel on the right hand-side now unfortunately interfere with each other, and I thought you might have a solution to this:
t = tiledlayout(4,1);
longYTickLabels_left = ["negative yticklabel left" "0" "positive yticklabel left" ];%edit: added
longYTickLabels_right = ["negative yticklabel right" "0" "positive yticklabel right"];%edit: added
for i = 1:4
nexttile
title("Tile"+i)
yyaxis left
plot(rand(1,10))
yticklabels(longYTickLabels_left)%edit: added
yyaxis right
plot(rand(1,10))
yticklabels(longYTickLabels_right)%edit: added
end
title(t,"Tiles")
xlabel(t,"x")
ylabel(t,"y left")
% Create invisible axes
axBig = axes(t.Parent);
axBig.Position = t.Position.*[1,1,1.05,1];
axBig.Color = 'none';
axBig.XTick = [];
axBig.YTick = [];
axBig.XColor='none';
axBig.YColor='none';
% Put ylabel on the right
axBig.YAxisLocation = 'right';
ylabel(axBig,'y right','Color','k');
Here's a modification that self-adjusts to the label sizes.
t = tiledlayout(4,1);
longYTickLabels_left = ["negative yticklabel left" "0" "positive yticklabel left" ];%edit: added
longYTickLabels_right = ["negative yticklabel right" "0" "positive yticklabel right"];%edit: added
for i = 1:4
nexttile
title("Tile"+i)
yyaxis left
plot(rand(1,10))
yticklabels(longYTickLabels_left)%edit: added
yyaxis right
plot(rand(1,10))
yticklabels(longYTickLabels_right)%edit: added
end
title(t,"Tiles")
xlabel(t,"x")
overlayAxis(t, 'y left','yright');
function axOverlay = overlayAxis(t, leftLabel, rightLabel)
% Create invisible axes
axOverlay= axes(t.Parent);
t.OuterPosition=axOverlay.Position;
axOverlay.Color = 'none';
pos=["left","right"];
labels={leftLabel, rightLabel};
for i=1:2
% Put ylabel on the right
yyaxis(axOverlay,pos(i))
axOverlay.XColor='none';
axOverlay.YColor='none';
axOverlay.XTick = [];
axOverlay.YTick = [];
ylabel(axOverlay,labels{i},'Color','k');
end
end

Sign in to comment.

Products

Release

R2022a

Asked:

on 10 Apr 2026

Edited:

on 12 Apr 2026

Community Treasure Hunt

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

Start Hunting!