Is there a way to insert a common ylabel to secondary axes in a tiled layout?

61 views (last 30 days)
Hi everyone,
Dabbling a bit in MATLAB for the past year, mostly for data analysis. I am currently using version 2020 a and recently started enjoying the tiledlayout functionality. I was wondering, if there is a way to insert a shared y-axis title for secondary y-axes? For example, in this code:
t = tiledlayout(2,2);
for i = 1:4
f = figure(1);
ax = nexttile;
yyaxis left
x = (1:10);
y = rand(1,10)*100;
plot(x,y)
yyaxis right
plot(x,sin(y))
end
xlabel(t,'X')
ylabel(t,'Y')
I would like to insert a ylabel for the right axes (lets say 'Y2') that is 'shared' just as 'Y' is for the left axes. I haven't found a solution that worked for me yet, all information I could find would either relate to tiled layouts or two y axes, I haven't found something that combines the two.
I hope you understand my problem, any help is highly appreciated.

Accepted Answer

Rishik Ramena
Rishik Ramena on 16 Mar 2021
You need not create a figure for each of your tile, instead you can have a common figure which can then later be labelled as shown.
close all;
fig = figure;
t = tiledlayout(2,2);
for i = 1:4
nexttile;
yyaxis left
x = (1:10);
y = rand(1,10)*100;
plot(x,y)
yyaxis right
plot(x,sin(y))
end
ax = axes(fig);
han = gca;
han.Visible = 'off';
% X label
xlabel('X')
han.XLabel.Visible = 'on';
% Left label
yyaxis(ax, 'left');
ylabel('Y1')
han.YLabel.Visible = 'on';
% Right label
yyaxis(ax, 'right');
ylabel('Y2')
han.YLabel.Visible = 'on';
  2 Comments
Manuel Bruch
Manuel Bruch on 16 Mar 2021
Thank you very much! This definitely does the trick. The left y-label was in my case overlapping with the axes of the plots next to it but by changing the label position that is no issue.

Sign in to comment.

More Answers (0)

Categories

Find more on Axes Appearance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!