Name legend on graph with a second y axis

3 views (last 30 days)
I am plotting three curves on the same figure, but I have not been able to name the third legend because the third curve is plotted against a second y axis. In the following code is the graph and the things I've tried
T_4340_ASM = [ 205
315
425
540
650]
T_4340_ASM = 5×1
205 315 425 540 650
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S_u_4340_ASM = [ 1980
1760
1500
1240
1020]
S_u_4340_ASM = 5×1
1980 1760 1500 1240 1020
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S_y_4340_ASM = [ 1860
1620
1365
1160
860]
S_y_4340_ASM = 5×1
1860 1620 1365 1160 860
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
El_4340_ASM = [ 11
12
14
17
20]
El_4340_ASM = 5×1
11 12 14 17 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
HB_4340_ASM = [ 520
490
440
360
290]
HB_4340_ASM = 5×1
520 490 440 360 290
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure(1)
plot(T_4340_ASM,S_y_4340_ASM,"Marker","square",Color=[0.6350 0.0780 0.1840])
hold on
plot(T_4340_ASM,S_u_4340_ASM,"Marker","pentagram","Color",[0 0.4470 0.7410])
hold on
title('Tempering temperature vs 4340 Steel Mechanical properties')
xlabel('Temperature[C°]')
ylabel('[MPa]')
legend('Yield Point','Tensile Strength') %If I put a third entry on here it ignores it because at this point the other curve is not plotted yet
yyaxis right
plot(T_4340_ASM,El_4340_ASM,"Marker","o") %And if i put it after here it just names the last legend
ylabel('Elongation [%]')

Accepted Answer

Walter Roberson
Walter Roberson on 27 Mar 2025
T_4340_ASM = [ 205
315
425
540
650]
T_4340_ASM = 5×1
205 315 425 540 650
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S_u_4340_ASM = [ 1980
1760
1500
1240
1020]
S_u_4340_ASM = 5×1
1980 1760 1500 1240 1020
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S_y_4340_ASM = [ 1860
1620
1365
1160
860]
S_y_4340_ASM = 5×1
1860 1620 1365 1160 860
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
El_4340_ASM = [ 11
12
14
17
20]
El_4340_ASM = 5×1
11 12 14 17 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
HB_4340_ASM = [ 520
490
440
360
290]
HB_4340_ASM = 5×1
520 490 440 360 290
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure(1)
h(1) = plot(T_4340_ASM,S_y_4340_ASM,"Marker","square",Color=[0.6350 0.0780 0.1840]);
hold on
h(2) = plot(T_4340_ASM,S_u_4340_ASM,"Marker","pentagram","Color",[0 0.4470 0.7410]);
hold on
title('Tempering temperature vs 4340 Steel Mechanical properties')
xlabel('Temperature[C°]')
ylabel('[MPa]')
legend('Yield Point','Tensile Strength') %If I put a third entry on here it ignores it because at this point the other curve is not plotted yet
yyaxis right
h(3) = plot(T_4340_ASM,El_4340_ASM,"Marker","o"); %And if i put it after here it just names the last legend
ylabel('Elongation [%]')
legend(h, {'S_y_4340_ASM', 'S_u_4340_ASM', 'El_4340_ASM'})
  2 Comments
Marol Valencia
Marol Valencia on 28 Mar 2025
Thanks! How can I change the color of the right y axis keeping the curve yellow?

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!