How do I make a secondary Y axis

5 views (last 30 days)
Kami S
Kami S on 31 Jan 2024
Commented: Sam Chak on 31 Jan 2024
I need to plot three graph against the same X axis, and to divide right Y axis into two parts as shown in the picture below.
This is how my code looks now, but the third axis is not displayed when the code is run:
figure
yyaxis left
x = [0.00;0.13;0.23;0.35;0.49;0.60;0.72;0.85;0.96;1.08;1.20];
y = [10.35;10.40;10.43;10.50;10.68;10.80;10.99;11.26;11.52;11.89;12.17];
plot(x,y, '-s')
ylabel('Head Ht (m)');
yyaxis right
y2 = [54.4;61.8;70.5;68.4;66.4;72.7;80.0;86.8;95.4;90.1;106.8];
plot(x,y2, '-o')
ylabel ('Power input Pm (W)')
hold on
y3= [0.0;21.9;32.8;51.9;77.4;86.9;96.3;107.8;113.1;138.8;133.2];
plot(x,y3,'-^g')
ylabel ('Pump efficiency (%)')
hold off
grid
xlabel('Volume flow rate Q (dm^3/s)')
legend('Head', 'Power','Efficiency', 'Location', 'NE')
  2 Comments
Sam Chak
Sam Chak on 31 Jan 2024
I added some comments in your code so that you understand what happened.
yyaxis left
... % business done on the left axis
yyaxis right % move to the right axis
y2 = [54.4;61.8;70.5;68.4;66.4;72.7;80.0;86.8;95.4;90.1;106.8];
plot(x,y2, '-o') % done plotting y2
ylabel ('Power input Pm (W)') % done labeling y2 on the right axis
hold on
y3= [0.0;21.9;32.8;51.9;77.4;86.9;96.3;107.8;113.1;138.8;133.2];
plot(x,y3,'-^g') % done plotting y3
ylabel ('Pump efficiency (%)') % labeling y3 overwrites y2 label
hold off
Kami S
Kami S on 31 Jan 2024
So how do place y2 and y3 on the right Y axis?
Thank you for your answer, it helped to understand what happened

Sign in to comment.

Accepted Answer

Sam Chak
Sam Chak on 31 Jan 2024
Moved: Sam Chak on 31 Jan 2024
Perhap a possible solution for manipulating the graph labeling is by using the yticks() command, which allows you to customize the labeling of the y-axis. If the intersections between the graphs are not significant or meaningful for your purpose, you might consider using the following plot instead. It could be a more suitable representation for your needs.
x = [0.00;0.13;0.23;0.35;0.49;0.60;0.72;0.85;0.96;1.08;1.20];
subplot(3,1,1)
y = [10.35;10.40;10.43;10.50;10.68;10.80;10.99;11.26;11.52;11.89;12.17];
plot(x, y, '-s', 'linewidth', 1.5, 'color', '#8D5524'), grid
ylabel('Head Ht (m)');
subplot(3,1,2)
y2 = [54.4;61.8;70.5;68.4;66.4;72.7;80.0;86.8;95.4;90.1;106.8];
plot(x, y2, '-o', 'linewidth', 1.5, 'color', '#C68642'), grid
ylabel ('Pwr input Pm (W)')
subplot(3,1,3)
y3= [0.0;21.9;32.8;51.9;77.4;86.9;96.3;107.8;113.1;138.8;133.2];
plot(x, y3, '-^', 'linewidth', 1.5, 'color', '#F1C27D'), grid
ylabel ('Pump eff (%)')
xlabel('Volume flow rate Q (dm^3/s)')
  2 Comments
Kami S
Kami S on 31 Jan 2024
Moved: Sam Chak on 31 Jan 2024
It would also work, thank you!
Sam Chak
Sam Chak on 31 Jan 2024
Hi @Kami S,
I'm glad to hear that you like the proposed graphing solution. The approach suggested by @Matt J has successfully addressed the technical aspects of your original question.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 31 Jan 2024
Edited: Matt J on 31 Jan 2024
It is, unfortunately, a very awkward thing to do. The datatip will show y3 with an increment of 200, but there may be ways to customize that.
yyaxis left
x = [0.00;0.13;0.23;0.35;0.49;0.60;0.72;0.85;0.96;1.08;1.20];
y = [10.35;10.40;10.43;10.50;10.68;10.80;10.99;11.26;11.52;11.89;12.17];
plot(x,y, '-s')
ylabel('Head Ht (m)');
yyaxis right
y2 = [54.4;61.8;70.5;68.4;66.4;72.7;80.0;86.8;95.4;90.1;106.8];
plot(x,y2, '-o')
ylabel ('Power input Pm (W) Pump efficiency (%) ') %<---- combine ylabels
hold on
y3= [0.0;21.9;32.8;51.9;77.4;86.9;96.3;107.8;113.1;138.8;133.2];
plot(x,y3+200,'-^g') %<---- y3 + 200
hold off
yticks([0:25:150,200:25:300]) %
yticklabels([string([0:25:150,0:10:40])]) %<---- manipulate yticks, yticklabels
ylim([0,350]) %
grid
xlabel('Volume flow rate Q (dm^3/s)')
legend('Head', 'Power','Efficiency', 'Location', 'NW')

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!