Make legend for lines

46 ビュー (過去 30 日間)
MINA
MINA 2016 年 5 月 22 日
コメント済み: MINA 2016 年 5 月 23 日
Hello, My plot has two many lines but with only two different colors. When I want to add the legend I just want to write the legend for those two colors, but in the legend it shows me the same color. Can anyone please tell me how I can skip the lines with the same color?
Here is my code
p1=line([t1 t1]',[0 1],'color','red');
hold on
p2=line([t2 t2]',[0 1],'color','green');
legend([p1 p2],'T1','T2')
  2 件のコメント
dpb
dpb 2016 年 5 月 23 日
If I give a value for t1, t2 and execute the above I get two colors for the legend.
Need something complete that causes the symptom we can run to try to duplicate the symptoms...
MINA
MINA 2016 年 5 月 23 日
t1=[1 2 3 4 5]'; t2=[3.5 6]'; p1=line([t1 t1]',[0 1],'color','red'); hold on p2=line([t2 t2]',[0 1],'color','green'); legend([p1 ;p2],'T1','T2')

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 5 月 23 日
linehandles = [p1, p2, p3, ...];
cols = cell2mat(get(linehandles, 'color'));
[~, uidx] = unique(cols, 'rows', 'stable');
legend(linehandles(uidx), {'first class', 'second class'})
  1 件のコメント
MINA
MINA 2016 年 5 月 23 日
Thanks. It worked.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by