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

조회 수: 83 (최근 30일)
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.

채택된 답변

Rishik Ramena
Rishik Ramena 2021년 3월 16일
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
Manuel Bruch
Manuel Bruch 2021년 3월 16일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by