How do I make a secondary Y axis

조회 수: 10 (최근 30일)
Kami S
Kami S 2024년 1월 31일
댓글: Sam Chak 2024년 1월 31일
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
Sam Chak
Sam Chak 2024년 1월 31일
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 2024년 1월 31일
So how do place y2 and y3 on the right Y axis?
Thank you for your answer, it helped to understand what happened

댓글을 달려면 로그인하십시오.

채택된 답변

Sam Chak
Sam Chak 2024년 1월 31일
이동: Sam Chak 2024년 1월 31일
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
Kami S
Kami S 2024년 1월 31일
이동: Sam Chak 2024년 1월 31일
It would also work, thank you!
Sam Chak
Sam Chak 2024년 1월 31일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2024년 1월 31일
편집: Matt J 2024년 1월 31일
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')

카테고리

Help CenterFile Exchange에서 Axes Appearance에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by