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

306 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

Accepted Answer

Adam Danz
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.
  46 Comments
Ashvinder Singh Gill Surmaish Singh Gill
Thank you for the information and input!
I just noticed, that the graph is not showing the final x=0mm and the corresponding y values is not shown. What seems to be missing?
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)
ax1=axes;
yyaxis left
yline(0,'-.');
[s1,al,idx] = unique(s1,'stable'); % identify the non duplicate values
f1 = f1(al);
hold on
plot(s1,f1,'*')
p4 = plot(nan,nan,'g-', 'DisplayName', sprintf('max=%.2f N',max(f1)))
p7 = plot(nan,nan,'g-', 'DisplayName', sprintf('min=%.4f N',min(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 % REMOVED AD
axis tight
p1 = plot(X_terp1,Y_terp1,'-g','DisplayName','1.4')
hold off
ytick = ax1.YTick;
yyaxis right
yline(0,'-.');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
hold on
axis tight % REMOVED AD
plot(s3,f3,'o')
p5 = plot(nan,nan,'r-', 'DisplayName', sprintf('max=%.2f N',max(f3)))
p8 = plot(nan,nan,'r-', 'DisplayName', sprintf('min=%.2f N',min(f3)))
theta_max = 2* pi ;
theta_min = 0 ;
theta = linspace(theta_min, theta_max,8)' ;
theta_3 = linspace(theta_min, theta_max,8)' ;
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 % REMOVED
axis tight % MOVED 2 LINES DOWN
p2 = plot(X_terp3,Y_terp3,'-r','DisplayName','2.2')
ylabel('Force (N)','color','k');
axis tight % ADDED - AD
ax2 = axes('position', ax1.Position);
[s2,al,idx] = unique(s2,'stable');
f2 = f2(al);
hold on
axis tight
plot(ax2,s2,f2,'+')
%ax.XAxisLocation = 'top'
%str = sprintf(formatSpec,A1,A2,A3)
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f N',max(f2)))
p9 = plot(nan,nan,'b-', 'DisplayName', sprintf('min=%.2f N',min(f2)))
p3 = plot(ax2,X_terp2,Y_terp2,'-b','DisplayName','1.6');
ylabel('Force(N)','color','k');
yline(0,'-.');
hold off
ax2.YTickLabel = strcat(ax2.YTickLabel, {' '}); % YOU CAN PLAY AROUND WITH THIS SPACING - AD
ax2.Color = 'none'; % ADDED - AD
xlabel('Displacement (mm)');
title('Force vs Displacement')
linkprop([ax1,ax2],'position')
lgd = legend([p1 p4 p7 p3 p6 p9 p2 p5 p8],'Orientation','horizontal','Location','southoutside','NumColumns',3)
title(lgd,'Width:Height Ratios')
lgd.Position(2) =0.01;
ax2.Position(4) = 0.65;
ax2.Position(2) = 0.27;
ax1.XTick = [];
ax1.YAxis(1).Color = 'g'; % or = [0 0 0] LEFT y-axis
ax1.YAxis(2).Color = 'r'; % or = [1 0 0] RIGHT y-axis
ax2.YAxis.Color = 'b';
grid on
Ashvinder Singh Gill Surmaish Singh Gill
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';

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!