MATLAB Answers

How to exclude some additional points from plot legend?

65 ビュー (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 10 月 27 日
The code generates plots in a loop and legend is given by an array contianing names. I am adding multiple single points at the end. How can I exclude these points from legend? I tried adding these points after addition of legened but legened showed points as well.

回答 (2 件)

Shubham Gupta
Shubham Gupta 2019 年 10 月 28 日
You can assign plot to different variables and when you use legend only use handles of the assigned variables. For e.g.
hold on;
l1 = plot(1:10);
l2 = plot(2:2:20);
l3 = plot(3:3:30);
Now, if you want legend only for l1 & l3 to show up. You can use:
legend([l1,l3],'Line-1','Line-3') % This will show legend for l1 & l3 only
I hope it helps !

ME 2019 年 10 月 28 日
An alternative to the answer of Shubham Gupta if you already have the figure plotted and you want to keep all of the points but simply remove some from the legend. If you select all of the items you want removed from the legend, go to "view>property editor" then click on "More Properties...". This will open a popout in which you can look for the "Handle Visibility" category - change this to "off" and the selected items will disappear from you legend.
You can also re-select them, go back to this menu and turn Handle Visibility back on if you later decide you want those items in your legend.
  1 件のコメント
Kristin Schall - van Bellen
Kristin Schall - van Bellen 2020 年 8 月 19 日
If the "Handle Visibility" category has the Value "on", is there a way to delet the points for the UIAxes?
I got the same problem as Zeynab but I don' know the exact number of lines that will be seen in the UIAxes an the legend, so the first answere is no option to me.
app.UIAxes.HandleVisibility='on' and cla(app.UIAxes) is not working...


Community Treasure Hunt

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

Start Hunting!

Translated by