different locations of y-axes and ylabels in subplots

10 Ansichten (letzte 30 Tage)
Sim
Sim am 19 Mai 2022
Kommentiert: Sim am 24 Mai 2022
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:

Akzeptierte Antwort

Midhulesh Vellanki
Midhulesh Vellanki am 20 Mai 2022
Bearbeitet: Midhulesh Vellanki am 20 Mai 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')

Weitere Antworten (0)

Kategorien

Mehr zu Axes Appearance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by