# How to add 3rd Y-axis at right side?

232 views (last 30 days)
I wish to add a 3rd y-axis on the right side to plot the blue line, Can anyone guide me how should I do it?
Thank you!
data = xlsread("sampleexceldata.xlsx") ;
K = data(1,[1 3 5]); % factors
data(1,:) = [];
s1 = data(2:20,1) ; f1 = data(2:20,2) ;
s2 = data(2:9,3) ; f2 = data(2:9,4) ;
s3 = data(2:11,5) ; f3 = data(2:11,6) ; % rows with numerical values, not with NAN
figure(1)
yyaxis right
ylabel('Force');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
[s2,al,idx] = unique(s2,'stable'); % identify the non duplicate values
f2 = f2(al);
hold on
axis tight
plot(s3,f3,'o')
plot(s2,f2,'+')
%axis ([0 5 -40 90])
theta_max = 2* pi ;
theta_min = 0 ;
theta = linspace(theta_min, theta_max,8)' ;
theta_3 = linspace(theta_min, theta_max,7)' ;
N_terp = 1024;
theta_2 = linspace(theta_min,theta_max,N_terp)' ;
X_terp3 = spline (theta, s3, theta_2);
Y_terp3 = spline (theta, f3,theta_2);
X_terp2 = spline (theta_3, s2, theta_2);
Y_terp2 = spline (theta_3, f2,theta_2);
hold on
axis tight
plot(X_terp3,Y_terp3,'-r')
plot(X_terp2,Y_terp2,'-b')
yyaxis left
ylabel('Force ');
[s1,al,idx] = unique(s1,'stable'); % identify the non duplicate values
f1 = f1(al);
%axis ([0 6 -15 15])
hold on
axis tight
plot(s1,f1,'*')
theta_max = 2* pi ;
theta_min = 0 ;
theta_4 = linspace(theta_min, theta_max,17)' ;
N_terp = 1024;
theta_2 = linspace(theta_min,theta_max,N_terp)' ;
X_terp1 = spline (theta_4, s1, theta_2);
Y_terp1 = spline (theta_4, f1,theta_2);
hold on
axis tight
plot(X_terp1,Y_terp1,'-g')
hold on
xlabel('Displacement (mm)');
title('Force vs Displacement')
grid on Adam Danz on 26 May 2020
Edited: Adam Danz on 26 May 2020
Follow this example of creating 3 y-axes with two on the left and one on the right. To set the duplicate axes on the right side instead, see the 3 steps I just added to that answer under "Changes needed to create the double y-axis on the right side instead of the left side"
If you have any trouble adapting your code to the example, show us what you've got in a comment below.
I referred to the x-axis documentation:
I found these lines of codes applied (See bottom). How do I adapt these lines correctly into my code? I tried putting before figure code line (after s3 = data(2:11,5) ; f3 = data(2:11,6) ;) and obtained as such: ax=gca;
ax.XAxisLocation='origin';
ax.YAxisLocation='origin';