フィルターのクリア

How to unify legend in plot command?

3 ビュー (過去 30 日間)
Harr
Harr 2022 年 2 月 23 日
コメント済み: Harr 2022 年 2 月 23 日
I have the following code making a line plots for 2 different groups. In the legend it dosent show legend per group (only the first group is correct). I wonder if there is some command can be added to each plot group to fix the problem? BTW i am using matlab2018b.
ax = gca;
ax.TickLength = [0.02 0.06];
set(gca,'XAxisLocation','top','YAxisLocation','left','ydir','reverse','XScale','log');
ax.XRuler.TickLabelGapOffset =-10;
ax.YLim = [0 20];
ax.XLim = [0 1];
ax.YDir= 'reverse';
x = [1 3 5 7];
y = [2 4 6 8];
z = [0.1 0.21 0.78 0.48];
plot([z;z], [x; y], 'LineWidth',3,'color',[0.9 0.4 0.5])
hold(gca,'on')
x = [2 8 9 14];
y = [3 9 14 20];
z = [0.9 0.1 0.71 0.38];
plot([z;z], [x; y], 'LineWidth',3,'color',[0.9 0.8 0.10])
hold(gca,'on')
legend("Group1_Red","Group2_Yellow")
hold(gca,'off')

採用された回答

VBBV
VBBV 2022 年 2 月 23 日
ax = gca;
ax.TickLength = [0.02 0.06];
set(gca,'XAxisLocation','top','YAxisLocation','left','ydir','reverse','XScale','log');
ax.XRuler.TickLabelGapOffset =-10;
ax.YLim = [0 20];
ax.XLim = [0 1];
ax.YDir= 'reverse';
x = [1 3 5 7];
y = [2 4 6 8];
z = [0.1 0.21 0.78 0.48];
h1 = plot([z;z], [x; y], 'LineWidth',3,'color',[0.9 0.4 0.5])
hold(gca,'on')
x = [2 8 9 14];
y = [3 9 14 20];
z = [0.9 0.1 0.71 0.38];
h2 = plot([z;z], [x; y], 'LineWidth',3,'color',[0.9 0.8 0.10])
hold(gca,'on')
legend([h1(1) h2(1)],{"Group1_Red","Group2_Yellow"})
hold(gca,'off')
Use handles in legend
  1 件のコメント
Harr
Harr 2022 年 2 月 23 日
Dear VBBV,
Thanks for your answer! The trick about naming each plot group to h1,h2...is very useful and solved my problem :)
Best regards
/HARR

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by