Name legend on graph with a second y axis

3 ビュー (過去 30 日間)
Marol Valencia
Marol Valencia 2025 年 3 月 27 日
コメント済み: Walter Roberson 2025 年 3 月 28 日
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 [%]')

採用された回答

Walter Roberson
Walter Roberson 2025 年 3 月 27 日
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 件のコメント
Marol Valencia
Marol Valencia 2025 年 3 月 28 日
Thanks! How can I change the color of the right y axis keeping the curve yellow?
Walter Roberson
Walter Roberson 2025 年 3 月 28 日
set(gca, 'YColor', 'k') %black

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by