You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to add 3rd Y-axis at right side?
306 views (last 30 days)
Show older comments
Ashvinder Singh Gill Surmaish Singh Gill
on 26 May 2020
Commented: Ashvinder Singh Gill Surmaish Singh Gill
on 3 Jun 2020
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
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
on 26 May 2020
Edited: Ashvinder Singh Gill Surmaish Singh Gill
on 26 May 2020
I tried and adapted the code, but it isn't as I desire. The scales are just not right. How do I adjust this?
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 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')
pause(0.1)
ax1.XTickMode = 'manual';
ax1.YTickMode = 'manual';
ax1.YLim = [min(ax1.YTick), max(ax1.YTick)];
ax1.XLimMode = 'manual';
grid(ax1,'on')
ytick = ax1.YTick;
ax2 =axes('position',ax1.Position)
plot(ax2, s2,f2,'+')
pause(0.1)
ax2.Color='none';
grid(ax2,'on')
ax2.XLim = ax1.XLim;
ax2.XTick = ax1.XTick;
ax2.YLimMode = 'manual';
yl = ax2.YLim;
ax2.YTick = linspace(yl(1), yl(2), length(ytick));
ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});
%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
I can't run your code which makes it a bit more difficult to troubleshoot but I do see what's probably causing the mistake.
You're plotting the right axis first. My example plots the left axis first. If you want to plot the right axis first you need to make some changes. For one, ax1=axes need to be after yyaxis right.
After making that change, execute each line of the code individually to see where the problems start to happen. Let me know if you get stuck again and it would be helpful to attach the data.
Ashvinder Singh Gill Surmaish Singh Gill
on 27 May 2020
Edited: Ashvinder Singh Gill Surmaish Singh Gill
on 27 May 2020
I am stuck at this piece of code. Can you kindly assist? I modified to do the left one first and then followed by the right side and this is what I am getting. What do I need to change? I have attached the excel data file as well.
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
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')
ax1.XTickMode = 'manual';
ax1.YTickMode = 'manual';
ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]
ax1.XLimMode = 'manual';
grid(ax1,'on')
ytick = ax1.YTick;
yyaxis right
%ylabel('Force');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
hold on
axis tight
plot(s3,f3,'o')
%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')
ax2 = axes('position', ax1.Position);
ax2.XLim = ax1.XLim;
ax2.XTick = ax1.XTick;
ax2.YLimMode = 'manual';
yl = ax2.YLim;
ax2.YTick = linspace(yl(1), yl(2), length(ytick));
ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});
[s2,al,idx] = unique(s2,'stable');
f2 = f2(al);
plot(ax2,s2,f2,'+')
plot(ax2,X_terp2,Y_terp2,'-b')
xlabel('Displacement (mm)');
title('Force vs Displacement')
grid on
Adam Danz
on 27 May 2020
Edited: Adam Danz
on 28 May 2020
I left a comment in all caps for every change I made followed by my innitials (AD). There were a lot of redundant lines of code.
You can play around with the label spacing to meet your needs.
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
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 % REMOVED AD
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 % REMOVED AD
axis tight
plot(X_terp1,Y_terp1,'-g')
% ax1.XTickMode = 'manual'; % REMOVED AD
% ax1.YTickMode = 'manual'; % REMOVED AD
ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]
% ax1.XLimMode = 'manual'; % REMOVED AD
grid(ax1,'on')
ytick = ax1.YTick;
yyaxis right
%ylabel('Force');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
hold on
% axis tight % REMOVED AD
plot(s3,f3,'o')
%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 % REMOVED
% axis tight % MOVED 2 LINES DOWN
plot(X_terp3,Y_terp3,'-r')
axis tight % ADDED - AD
ax2 = axes('position', ax1.Position);
[s2,al,idx] = unique(s2,'stable');
f2 = f2(al);
plot(ax2,s2,f2,'+')
% plot(ax2,X_terp2,Y_terp2,'-b') % REMOVED ONLY BECAUSE I DON'T HAVE THOSE VALUES - AD
% THESE 6 LINES MOVED FROM UNDER ax2 = axes()
ax2.XLim = ax1.XLim;
ax2.XTick = ax1.XTick;
ax2.YLimMode = 'manual';
yl = ax2.YLim;
ax2.YTick = linspace(yl(1), yl(2), length(ytick));
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')
grid on
Ashvinder Singh Gill Surmaish Singh Gill
on 28 May 2020
Edited: Ashvinder Singh Gill Surmaish Singh Gill
on 28 May 2020
I am one step closer. In your previous code, the blue color plot were neglected.
Now , my question is how do I remove the double x-axis values that are overlapping in the image below, change the color of all the y-axes and show horizontal lines at the maximum y for each plots with the value being labelled?
Appreciate if you can enlighten please.
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);
%axis ([0 6 -15 15])
hold on
% axis tight % REMOVED AD
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 % REMOVED AD
axis tight
p1 = plot(X_terp1,Y_terp1,'-k')
hold off
%ax1.XTickMode = 'manual'; % REMOVED AD
%ax1.YTickMode = 'manual'; % REMOVED AD
%ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]
%ax1.XLimMode = 'manual'; % REMOVED AD
%grid(ax1,'on')
ytick = ax1.YTick;
yyaxis right
yline(0,'-.');
ylabel('Force(N)','color','k');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
hold on
axis tight % REMOVED AD
plot(s3,f3,'o')
%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,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')
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,'+')
p3 = plot(ax2,X_terp2,Y_terp2,'-b')
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
% REMOVED ONLY BECAUSE I DON'T HAVE THOSE VALUES - AD
% THESE 6 LINES MOVED FROM UNDER ax2 = axes()
%ax2.XLim = ax1.XLim;
%ax2.XTick = ax1.XTick;
%ax2.YLimMode = 'manual';
%yl = ax2.YLim;
%ax2.YTick = linspace(yl(1), yl(2), length(ytick));
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')
lgd = legend([p1 p3 p2],{'1.4','1.6','2.2'},'Location','southeast')
title(lgd,'Width Ratios')
grid on
Adam Danz
on 28 May 2020
Edited: Adam Danz
on 28 May 2020
my question is how do I remove the double x-axis values that are overlapping
ax1.XTick = [];
change the color of all the y-axes
% For the first pair of axes using yyaxis,
ax1.YAxis(1).Color = 'k'; % or = [0 0 0] LEFT y-axis
ax1.YAxis(2).Color = 'r'; % or = [1 0 0] RIGHT y-axis
% For 2nd axis,
ax2.YAxis.Color = 'b';
show horizontal lines at the maximum y for each plots with the value being labelled?
See yline() funciton.
Ashvinder Singh Gill Surmaish Singh Gill
on 28 May 2020
show horizontal lines at the maximum y for each plots with the value being labelled?
I have seen the yline() function. It does not help.
What I wish to do is that I want to see the ymax line automatically respectively for all the plots at displacement=5mm. Hence, How can I do this?
Thanks for the previous advise. It worked.
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);
%axis ([0 6 -15 15])
hold on
% axis tight % REMOVED AD
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 % REMOVED AD
axis tight
p1 = plot(X_terp1,Y_terp1,'-g')
hold off
%ax1.XTickMode = 'manual'; % REMOVED AD
%ax1.YTickMode = 'manual'; % REMOVED AD
%ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]
%ax1.XLimMode = 'manual'; % REMOVED AD
%grid(ax1,'on')
ytick = ax1.YTick;
yyaxis right
yline(0,'-.');
ylabel('Force(N)','color','k');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
hold on
axis tight % REMOVED AD
plot(s3,f3,'o')
%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,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')
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,'+')
p3 = plot(ax2,X_terp2,Y_terp2,'-b')
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
% REMOVED ONLY BECAUSE I DON'T HAVE THOSE VALUES - AD
% THESE 6 LINES MOVED FROM UNDER ax2 = axes()
%ax2.XLim = ax1.XLim;
%ax2.XTick = ax1.XTick;
%ax2.YLimMode = 'manual';
%yl = ax2.YLim;
%ax2.YTick = linspace(yl(1), yl(2), length(ytick));
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')
lgd = legend([p1 p3 p2],{'1.4','1.6','2.2'},'Location','southeast')
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 28 May 2020
Glad I could help!
I think you're looking for
box on
or, to specify the axes,
box(ax2,'on')
But that won't add the tick label at the top of the axes. To add the y-tick label at the top of the axes,
ax2.YTick = unique([ax2.YTick, max(ylim(ax2))]);
You can replace the 3 ax2 handles with the axis handles you'd like to change.
Ashvinder Singh Gill Surmaish Singh Gill
on 28 May 2020
I am looking to plot a horizontal line at the maximum y values for each of the plots. The lines of code that you've mentioned are not plotting any horizontal line. Does this clarify?
Adam Danz
on 28 May 2020
It should do what you're asking.
In the figure below, the right axis is with box on and the left axis is without that command. You might not be applying it to the correct axes.
Alternatively, you can use yline as I mentioned earlier. (replace the 2 ax2 with your axis handles).
yline(ax2, max(ylim(ax2)), '-k')
Ashvinder Singh Gill Surmaish Singh Gill
on 28 May 2020
I think you got me wrong. Let me clarify, I would like to show the maximum y values that is obtained at x=5mm for each of the plots. It has nothing relevant to as shown in your comment above. I hope it is understood?
Adam Danz
on 28 May 2020
Ah, that's very different from "the maximum y values for each of the plots" which describes max(ylim). You want the maximum y value of the data.
It's also unclear what you mean by "show horizontal lines...". If you want the line to extend across the entire axes, then you would use yline(). All you need is the max value of y in the data.
If you're plotting with plot(x,y), then the max value of y is max(y) and you could add the line using
yline(ax2, max(y), 'k-', y)
% or
yline(ax2, max(y), 'k-', round(y,2)) % to round the label to 2 dp.
You just have to replace y with your variable and ax2 with your axis handle.
Ashvinder Singh Gill Surmaish Singh Gill
on 28 May 2020
Yes it worked, how do I show the y value as well in addition to the line?
Adam Danz
on 28 May 2020
Edited: Adam Danz
on 28 May 2020
The code I shared adds the y-value label on top of the line, on the right of the plot. If the line is at the top of your plot, the label is probably cut off. In that case, set the LabelVerticalAlignment to botom.
yline(. . . , 'LabelVerticalAlignment', 'bottom')
If you do not want the y value to show,
yline(ax2, y, '-k')
Ashvinder Singh Gill Surmaish Singh Gill
on 29 May 2020
Edited: Ashvinder Singh Gill Surmaish Singh Gill
on 29 May 2020
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
It says as such. What should I be modifying?
yline(ax2, max(f2), 'y-', f2, 'LabelVerticalAlignment', 'bottom')
Ashvinder Singh Gill Surmaish Singh Gill
on 29 May 2020
Thanks it worked.
Now, How to show some additional space above the ylines plotted? Say about 2-3 additional grid empty above the lines to give better visibility.
Ashvinder Singh Gill Surmaish Singh Gill
on 29 May 2020
Here the entire line of code. I have no idea how could I do it?
As you can see the values at maximum y are overlapping and is definitely not a good practise.I want to position them well so that they are visible clearly.
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);
%axis ([0 6 -15 15])
hold on
% axis tight % REMOVED AD
plot(s1,f1,'*')
yline( max(f1), 'g-', max(f1) , 'LabelVerticalAlignment', 'bottom')
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')
hold off
%ax1.XTickMode = 'manual'; % REMOVED AD
%ax1.YTickMode = 'manual'; % REMOVED AD
%ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]
%ax1.XLimMode = 'manual'; % REMOVED AD
%grid(ax1,'on')
ytick = ax1.YTick;
yyaxis right
yline(0,'-.');
ylabel('Force(N)','color','k');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
hold on
axis tight % REMOVED AD
plot(s3,f3,'o')
yline( max(f3), 'r-', max(f3) , 'LabelVerticalAlignment', 'bottom')
%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,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')
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,'+')
yline(ax2, max(f2), 'b-', max(f2) , 'LabelVerticalAlignment', 'bottom')
p3 = plot(ax2,X_terp2,Y_terp2,'-b')
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
% REMOVED ONLY BECAUSE I DON'T HAVE THOSE VALUES - AD
% THESE 6 LINES MOVED FROM UNDER ax2 = axes()
%ax2.XLim = ax1.XLim;
%ax2.XTick = ax1.XTick;
%ax2.YLimMode = 'manual';
%yl = ax2.YLim;
%ax2.YTick = linspace(yl(1), yl(2), length(ytick));
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')
lgd = legend([p1 p3 p2],{'1.4','1.6','2.2'},'Location','southeast')
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 29 May 2020
In that case, the line labels might not be the best idea.
You could mark the max vals in a legend.
% Plotting (nan,nan) does not add anything to the plot
% but you can use it to add an item to the legend which
% uses the name in 'DisplayName'.
% Set the line color and the max value (max(y))
p1 = plot(nan,nan,'r-', 'DisplayName', sprintf('max=%.2f',max(y)))
% Do that for all 3 lines
p2 = ...
p3 = ...
% Then add the legend
legend([p1,p2,p3]) % it will use the 'DisplayNames'
Ashvinder Singh Gill Surmaish Singh Gill
on 29 May 2020
Yes this is great too but my other legend for 1.4,1.6,2.2 is gone. See the image in the previous comment. How do I include that as well?
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);
%axis ([0 6 -15 15])
hold on
% axis tight % REMOVED AD
plot(s1,f1,'*')
%yline( max(f1), 'g-', max(f1) , 'LabelVerticalAlignment', 'bottom')
p4 = plot(nan,nan,'g-', 'DisplayName', sprintf('max=%.2f',max(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')
hold off
%ax1.XTickMode = 'manual'; % REMOVED AD
%ax1.YTickMode = 'manual'; % REMOVED AD
%ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]
%ax1.XLimMode = 'manual'; % REMOVED AD
%grid(ax1,'on')
ytick = ax1.YTick;
yyaxis right
yline(0,'-.');
ylabel('Force(N)','color','k');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
hold on
axis tight % REMOVED AD
plot(s3,f3,'o')
%yline( max(f3), 'r-', max(f3) , 'LabelVerticalAlignment', 'bottom')
p5 = plot(nan,nan,'r-', 'DisplayName', sprintf('max=%.2f',max(f3)))
%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,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')
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,'+')
%yline(ax2, max(f2), 'b-', max(f2) , 'LabelVerticalAlignment', 'bottom')
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f',max(f2)))
p3 = plot(ax2,X_terp2,Y_terp2,'-b')
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
% REMOVED ONLY BECAUSE I DON'T HAVE THOSE VALUES - AD
% THESE 6 LINES MOVED FROM UNDER ax2 = axes()
%ax2.XLim = ax1.XLim;
%ax2.XTick = ax1.XTick;
%ax2.YLimMode = 'manual';
%yl = ax2.YLim;
%ax2.YTick = linspace(yl(1), yl(2), length(ytick));
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')
lgd = legend([p1 p3 p2],{'1.4','1.6','2.2'},'Location','southeast')
title(lgd,'Width:Height Ratios')
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';
legend([p4 p5 p6],'Location','northwest')
grid on
Adam Danz
on 29 May 2020
Two steps.
1) in the lines of code that plot something that you want to appear on the legend, you need thier output handle (which you already have) and you need to set their DisplayName which I've demonstrated.
2) When you produce the legend, just supply a vector of all handles as you see in my previous comment. You can also add 'Location', 'NorthWest' or whatever other additional inputs you want.
Ashvinder Singh Gill Surmaish Singh Gill
on 30 May 2020
I tried, I am unable to yield it or either I am misinterpreting. Is there anything I need to modify here?
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);
%axis ([0 6 -15 15])
hold on
% axis tight % REMOVED AD
plot(s1,f1,'*')
%yline( max(f1), 'g-', max(f1) , 'LabelVerticalAlignment', 'bottom')
p4 = plot(nan,nan,'g-', 'DisplayName', sprintf('max=%.2f',max(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')
hold off
%ax1.XTickMode = 'manual'; % REMOVED AD
%ax1.YTickMode = 'manual'; % REMOVED AD
%ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]
%ax1.XLimMode = 'manual'; % REMOVED AD
%grid(ax1,'on')
ytick = ax1.YTick;
yyaxis right
yline(0,'-.');
ylabel('Force(N)','color','k');
[s3,al,idx] = unique(s3,'stable'); % identify the non duplicate values
f3 = f3(al);
hold on
axis tight % REMOVED AD
plot(s3,f3,'o')
%yline( max(f3), 'r-', max(f3) , 'LabelVerticalAlignment', 'bottom')
p5 = plot(nan,nan,'r-', 'DisplayName', sprintf('max=%.2f',max(f3)))
%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,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')
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,'+')
%yline(ax2, max(f2), 'b-', max(f2) , 'LabelVerticalAlignment', 'bottom')
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f',max(f2)))
p3 = plot(ax2,X_terp2,Y_terp2,'-b')
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
% REMOVED ONLY BECAUSE I DON'T HAVE THOSE VALUES - AD
% THESE 6 LINES MOVED FROM UNDER ax2 = axes()
%ax2.XLim = ax1.XLim;
%ax2.XTick = ax1.XTick;
%ax2.YLimMode = 'manual';
%yl = ax2.YLim;
%ax2.YTick = linspace(yl(1), yl(2), length(ytick));
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')
lgd_2 = legend([p4 p5 p6],'Location','northwest')
title(lgd_2,'Maximum Values')
lgd = legend([p1 p3 p2 ],{'1.4','1.6','2.2'},'Location','southeast')
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 30 May 2020
You're calling legend() twice. The second time you call it, it overrides the first legend produced.
For every line in your plot, assign it a DisplayName. Here's an example.
p3 = plot(ax2,X_terp2,Y_terp2,'-b', 'DisplayName', '1.6');
Then call legend once with all of the handles you'd like to appear in the legend.
lgd = legend([p1 p3 p2 p4 p5 p6],'Location','northwest')
Ashvinder Singh Gill Surmaish Singh Gill
on 30 May 2020
It worked.
Now, I want to show the max values with the unit of N, example ( 11.51 N) and I tried placing the box outside of the plot (means: Outside the entire plotting box), Is there anything I need to change, I used 'eastoutside' but it shows as follow. Please advise if there is change.
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',max(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,'-.');
ylabel('Force(N)','color','k');
[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',max(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')
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,'+')
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f',max(f2)))
p3 = plot(ax2,X_terp2,Y_terp2,'-b','DisplayName','1.6');
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
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')
lgd = legend([p1 p3 p2 p4 p5 p6],'Location','eastoutside','NumColumns',2)
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 30 May 2020
Edited: Adam Danz
on 30 May 2020
See sprintf() or compose() functions to add N to your labels.
One of the 'outside' options for the legend location should put the legend outside of the plotting plot. I can't imagine why that wouldn't work.
Ashvinder Singh Gill Surmaish Singh Gill
on 30 May 2020
This picture shows what I am getting. I used 'eastoutside' and the following snippet.
A3 = "N";
%str = sprintf(formatSpec,A1,A2,A3)
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f',max(f2),A3))
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',max(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,'-.');
ylabel('Force(N)','color','k');
[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',max(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')
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,'+')
A3 = "N";
%str = sprintf(formatSpec,A1,A2,A3)
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f',max(f2),A3))
p3 = plot(ax2,X_terp2,Y_terp2,'-b','DisplayName','1.6');
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
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')
lgd = legend([p1 p3 p2 p4 p5 p6],'Location','eastoutside','NumColumns',2)
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 30 May 2020
I see what the problem is. When you apply the legend to one of the axes, it adjusts the axis position but the other axis position is not also adjusted.
Before assigning the legend, link the two axes positions so that when one changes, the other also changes.
linkprop([ax1,ax2],'position')
You can link any other properties you'd like.
Ashvinder Singh Gill Surmaish Singh Gill
on 30 May 2020
I have tried and obtained as such but the graph is becoming smaller.
How to retain it as per the original and to show unit 'N' for the max values?
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',max(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,'-.');
ylabel('Force(N)','color','k');
[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',max(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')
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,'+')
linkprop([ax1,ax2],'position')
A3 = "N";
%str = sprintf(formatSpec,A1,A2,A3)
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f',max(f2),A3))
p3 = plot(ax2,X_terp2,Y_terp2,'-b','DisplayName','1.6');
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
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')
lgd = legend([p1 p3 p2 p4 p5 p6],'Location','eastoutside')
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 30 May 2020
If you want the legend to appear outside of the axes, the additional space it will consume needs to come from somwhere.
I suggest
- put the legend on the bottom (southoutside)
- Set the orientation of the legend to horizontal (see documentation)
- And then set the number of columns of the legend to 3 which will create a 2x3 legend (two rows). See "NumColumns" property of the legend.
For the 'N' units, adapt this line below as needed.
plot(. . ., 'DisplayName', sprintf('%.1f N', value)) % '%.1f' shows 1 decimal place
Ashvinder Singh Gill Surmaish Singh Gill
on 30 May 2020
Edited: Ashvinder Singh Gill Surmaish Singh Gill
on 30 May 2020
Well said!
However, how do I space the southoutside legend table a little bit further down so that it does not overlap the 'Displacement (mm)' label?
Also, do comment on my overall coding, is there any thing that I can reduce improve. I see a very long lines of code for such an example.
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)))
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,'-.');
ylabel('Force(N)','color','k');
[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)))
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')
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,'+')
linkprop([ax1,ax2],'position')
%str = sprintf(formatSpec,A1,A2,A3)
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f N',max(f2)))
p3 = plot(ax2,X_terp2,Y_terp2,'-b','DisplayName','1.6');
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
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')
lgd = legend([p1 p3 p2 p4 p5 p6],'Orientation','horizontal','Location','southoutside','NumColumns',3)
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 30 May 2020
To fix that, you need to change the position of one of the axes. Since you've used linkprops to link the axes positions, you just need to change one of them.
% This line will reduce the height of the axis to 90% of its current height
ax1.Position(4) = ax.Position(4)*.90;
% This line will shift the axis up ward by 10%
ax1.Position(2) = ax.Position(2)*1.1;
I maded up those values, you can play with them to get it right. Assuming normalized units.
Note, you can also change the position of the legend to make sure it's at the very bottom.
leg.Position(2) = 0.001; % leg is the handle to the legend.
Ashvinder Singh Gill Surmaish Singh Gill
on 30 May 2020
I adapted all of these lines above. But, those lines are not helping in shifting the legend to below the 'Displacement (mm)' label.
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)))
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,'-.');
ylabel('Force(N)','color','k');
[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)))
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')
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,'+')
linkprop([ax1,ax2],'position')
ax1.Position(4) = ax1.Position(4)*1;
ax1.Position(2) = ax1.Position(2)*0.5;
leg.Position(2) = 5;
%str = sprintf(formatSpec,A1,A2,A3)
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f N',max(f2)))
p3 = plot(ax2,X_terp2,Y_terp2,'-b','DisplayName','1.6');
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
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')
lgd = legend([p1 p3 p2 p4 p5 p6],'Orientation','horizontal','Location','southoutside','NumColumns',3)
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 30 May 2020
It doesn't look like you've applied the correct offsets.
% Your code
ax1.Position(4) = ax1.Position(4)*1; % Line 1
ax1.Position(2) = ax1.Position(2)*0.5; % Line 2
leg.Position(2) = 5; % Line 3
Line 1 just multiplies the height of the axes by 1 so that doesn't change anything. You want to decrease the height so you have to mupltiply the current height by some value < 1.
Line 2 decreases the vertical position of the axes. You want to do the opposite. You want the increase the vertical position by multiplying the current position by some value > 1.
Line 3 doesn't seem right. I think the legend position units are normalized. You can veryify this by looking at the legend position units. If they are between [0,1] they are normalized. My example set the vertical position of the legend to the very bottom.
Ashvinder Singh Gill Surmaish Singh Gill
on 31 May 2020
Line 3 seems highly possible, but this is what it results when I use 0.001.
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)))
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,'-.');
ylabel('Force(N)','color','k');
[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)))
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')
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,'+')
linkprop([ax1,ax2],'position')
ax1.Position(4) = ax1.Position(4)*1;
ax1.Position(2) = ax1.Position(2)*0.5;
leg.Position(2) = 0.001;
%str = sprintf(formatSpec,A1,A2,A3)
p6 = plot(nan,nan,'b-', 'DisplayName', sprintf('max=%.2f N',max(f2)))
p3 = plot(ax2,X_terp2,Y_terp2,'-b','DisplayName','1.6');
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
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')
lgd = legend([p1 p3 p2 p4 p5 p6],'Orientation','horizontal','Location','southoutside','NumColumns',3)
title(lgd,'Width:Height Ratios')
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
Adam Danz
on 31 May 2020
Take a moment to understand what each line is doing. The position vector has 4 elements. [distance from the left, distance from the bottom, width, height]. The values are normalized 0:1. This is all explained in the documentation if you look up "axis properties".
The idea is to move the legend to the very bottom by changing position #2 of the legend. Then you have to move the axis up by changing position #2 of the axis. But since you moved the legend up, now you have to decrease the height which is position #4. Play around with those number and watch how the plot reacts. Then fine-tune it until you get positions that work for you.
Ashvinder Singh Gill Surmaish Singh Gill
on 31 May 2020
Is this the line of code that I need to change in terms of value? I play around with the values but, this particular line is not causing any difference.
Please advise.
leg.Position(4) = -5;
Here is the complete line that I am using:
ax1.Position(2) = ax1.Position(2)*0.5;
leg.Position(4) = -5;
Adam Danz
on 1 Jun 2020
leg.Position(4) = -5;
Again, take time to think about the commands.
Position 4 is the height as I mentioned in my previous comment. Height cannot be negative. Also, since the values are normalized, they should always be between 0:1.
ax1.Position(2) = ax1.Position(2)*0.5;
Position 2 is the distance between the bottom of the axis and the bottom of the figure. If you multiply that value by 0.5, the axes will move downward by 1/2 the distance.
Read about the position property here https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#d120e59275
Ashvinder Singh Gill Surmaish Singh Gill
on 1 Jun 2020
I tried. I am finding difficulties in adapting it. Is there possibility if I can make the legend box smaller so that it fits nicely in the plot?
Adam Danz
on 1 Jun 2020
I just looked at your most recent code above and it has some logical flaws. The reason you're having problems with this is because you haven't taken the time to understand what each line of code is doing. This is a common mistake for beginners. If you invest x-minutes in understanding the code, you'll save at least x*10 minutes when troubleshooting it.
For example, this line of your code does nothing.
leg.Position(2) = 0.001;
- The line is called before your legend is even produced.
- Your legend is stored in 'ldg', not 'leg'. So, that line of code has no effect on the legend.
Here's what needs to happen to produce the layout below. All of these steps should come after all plotting and axis labeling etc.
- Link the axes positions after all of your plotting and labeling is done. linkprop([ax1,ax2],'position').
- Add the legend to the bottom. Your legend inputs look OK but the legend shoudld be added after you're done with plotting and labeling and after using linkprop().
- . Move the legend downward by setting the 2nd position element of the legend to 0.01.
- Set position #4 to ax2 to 0.65.
- Set position #2 to ax2 to 0.27
Ashvinder Singh Gill Surmaish Singh Gill
on 1 Jun 2020
If I interpretted the information on the steps correctly, this is how I am getting now.
Is there a mistake in the code sequence for the steps that you've mentioned?
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)))
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,'-.');
ylabel('Force(N)','color','k');
[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)))
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')
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)))
p3 = plot(ax2,X_terp2,Y_terp2,'-b','DisplayName','1.6');
yline(0,'-.');
hold off
ylabel('Force (N)','color','k');
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 p3 p2 p4 p5 p6],'Orientation','horizontal','Location','southoutside','NumColumns',3)
title(lgd,'Width:Height Ratios')
lgd.Position(2) =0.01;
ax.Position(4) = ax2.Position(4)*0.65;
ax.Position(2) = 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
Adam Danz
on 1 Jun 2020
Much closer. Two mistakes.
1) you're applying the position changes to 'ax' which isn't one of your axis handles. You want to apply the changes to ax2.
2) Instead of changing the positions using a factor (as we've previously discussed), my latest suggestion is to set the propery directly. ie: a2.Position(4) = 0.65;
Ashvinder Singh Gill Surmaish Singh Gill
on 1 Jun 2020
Lovely it worked! Thank you so much! Appreciate it!
Please comment on my entire code. Is there anything that I need to change or improve to make it look cleaner and simple?
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)))
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)))
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)))
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 p3 p2 p4 p5 p6],'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
Adam Danz
on 2 Jun 2020
See my comments that begin with "*". I just commented on the first half of the code.
% * Use readmatrix() instead of xlsread()
data = xlsread("sampleexceldata.xlsx") ;
% * Is almost always a bad idea to break apart a matrix or a table into separate variables
% * Use indexing instead. For example, instead of K, just use data(1,[1,3,5]).
K = data(1,[1 3 5]); % factors
% * Again, the line below is not needed.
data(1,:) = [];
% * Again, it's always better to use indexing rather than splitting the organized
% matrix or table apart.
% * Also, it's difficult for other people to read code that has more than 1 line
% of code per line of text. But f1, f2 f3 in separate lines.
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
% * What if figure 1 is alread used? This will add an additional axes to it.
% Better to use figure() or clf().
figure(1)
ax1=axes;
yyaxis left
yline(0,'-.');
[s1,al,idx] = unique(s1,'stable'); % identify the non duplicate values
f1 = f1(al); % * Again, keep the data together and use indexing. f1 is just data(f1+1,3)
hold on
plot(s1,f1,'*')
p4 = plot(nan,nan,'g-', 'DisplayName', sprintf('max=%.2f N',max(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 % * You already set hold=on, no need to do it again for the same axes
axis tight
p1 = plot(X_terp1,Y_terp1,'-g','DisplayName','1.4')
hold off
ytick = ax1.YTick;
Ashvinder Singh Gill Surmaish Singh Gill
on 2 Jun 2020
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
on 3 Jun 2020
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';
More Answers (0)
See Also
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)