フィルターのクリア

How to show partial legend in figure

394 ビュー (過去 30 日間)
Aravin
Aravin 2012 年 1 月 5 日
コメント済み: Julien 2024 年 6 月 8 日
Dear All,
I just want to show the partial part of my legend in Figure.
For example.
plot([1:10],'Color','r','DisplayName','This one');hold on;
plot([1:2:10],'Color','b','DisplayName','This two');
plot([1:3:10],'Color','k','DisplayName','This three');
Lets say I want to display only Last two or First two or first and last legend titles with proper colors.
Could anyone suggest plz.

採用された回答

Dr. Seis
Dr. Seis 2012 年 1 月 5 日
To take your first example:
h = zeros(1,3);
h(1) = plot([1:10],'Color','r','DisplayName','This one');hold on;
h(2) = plot([1:2:10],'Color','b','DisplayName','This two');
h(3) = plot([1:3:10],'Color','k','DisplayName','This three'); hold off;
legend(h(2:3)); % Only display last two legend titles
If you didn't have "DisplayName", then you would have to manually add these string to the legend:
legend(h(2:3),'This two','This three');

その他の回答 (3 件)

Jon
Jon 2017 年 4 月 13 日
編集済み: Jon 2017 年 4 月 13 日
Posting this here because none of the above helped me. If you don't have control of how the figure was plotted (i.e. it was buried in someone else's code), you can pull it out of the figure's children. So if you have 6 graphs and only want the legend to display a certain two, then write:
f=get(gca,'Children');
legend([f(2),f(6)],'second graph','sixth graph')
  11 件のコメント
YU CHEN
YU CHEN 2021 年 6 月 7 日
Perfect!Thank you very much!
Julien
Julien 2024 年 6 月 8 日
Ya da best!

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


the cyclist
the cyclist 2012 年 1 月 5 日
Here's one way:
h1=plot([1:10],'Color','r','DisplayName','This one');hold on;
h2=plot([1:2:10],'Color','b','DisplayName','This two');
h3=plot([1:3:10],'Color','k','DisplayName','This three');
legend([h1 h3],{'Legend 1','Legend 3'})
  1 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 1 月 5 日
+1. This should probably be added to the FAQ.

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


Patrick Kalita
Patrick Kalita 2012 年 1 月 5 日
the cyclist's solution of passing plot handles to the legend command is good. But if you're feeling adventurous, you can also use the plot's Annotation property. It is described in the documentation here. For example:
h(1) = plot([1:10],'Color','r','DisplayName','This one');hold on;
h(2) = plot([1:2:10],'Color','b','DisplayName','This two');
h(3) = plot([1:3:10],'Color','k','DisplayName','This three');
set( get( get( h(2), 'Annotation'), 'LegendInformation' ), 'IconDisplayStyle', 'off' );
legend show

カテゴリ

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