different locations of y-axes and ylabels in subplots

1 view (last 30 days)
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:

Accepted Answer

Midhulesh Vellanki
Midhulesh Vellanki on 20 May 2022
Edited: Midhulesh Vellanki on 20 May 2022
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')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!