different locations of y-axes and ylabels in subplots

2 ビュー (過去 30 日間)
Sim
Sim 2022 年 5 月 19 日
コメント済み: Sim 2022 年 5 月 24 日
I have 4 subplots and I would like to have:
  1. all the 4 ylabel texts (here called as "first", "second", "third" and "fourth") perfectly aligned on the left of the figure
  2. the Y-Axis (i.e. the vertical line, plus yticks and the corresponding numbers/values) of the first and third subplots on the right side of the figure
Is it possible?
Here you are an attempt (does not fullfill conditions 1 and 2):
subplot(4,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
ylabel('first')
hAxis = gca;
hAxis.YAxisLocation = 'right';
subplot(4,1,2);
y2 = sin(5*x);
plot(x,y2)
ylabel('second')
subplot(4,1,3);
x = linspace(0,10);
y1 = cos(x);
plot(x,y1)
ylabel('third')
hAxis = gca;
hAxis.YAxisLocation = 'right';
subplot(4,1,4);
y2 = sin(2*x);
plot(x,y2)
ylabel('fourth')
which results in:
My desired output is here represented:

採用された回答

Midhulesh Vellanki
Midhulesh Vellanki 2022 年 5 月 20 日
編集済み: Midhulesh Vellanki 2022 年 5 月 20 日
You can do something like the code below. please note that Ylabel is a child of YAxis that's why the label move along with the axis.
You can play with Positon proptery of label to further customize the behaviour.
subplot(4,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
la1=ylabel('first');
old_values1 = la1.Position;
hAxis = gca;
hAxis.YAxisLocation = 'right';
la1.Position = [x(1)-0.7 old_values1(2) old_values1(3)];
box off;
subplot(4,1,2);
y2 = sin(5*x);
plot(x,y2)
la2=ylabel('second');
subplot(4,1,3);
x = linspace(0,10);
y1 = cos(x);
plot(x,y1)
la3=ylabel('third');
old_values3 = la3.Position;
hAxis = gca;
hAxis.YAxisLocation = 'right';
la3.Position = [x(1)-0.7 old_values3(2) old_values3(3)];
box off;
subplot(4,1,4);
y2 = sin(2*x);
plot(x,y2)
ylabel('fourth')
  1 件のコメント
Sim
Sim 2022 年 5 月 24 日
Thanks a lot @Midhulesh Vellanki !! Very very kind! :-)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by