To exclude items from a legend, you can specify the objects that you want to include as the first input argument to the “legend” function.
For example, plot three lines and return the “Line” objects as array “p”. Include only the first and third lines in the legend by specifying “p(1)” and “p(3)” as the first input argument to “legend”.
p = plot(rand(3));
legend([p(1) p(3)],'plot 1','plot 3')
Alternatively, you can set the “IconDisplayStyle” to “off” for the object that you do not want to include in the legend. For example, exclude the second “Line” object, “p(2)”.
p = plot(rand(3));
set(get(get(p(2),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');
legend('plot 1','plot 3')
3 Comments
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/406-how-do-i-skip-items-in-a-legend#comment_544634
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/406-how-do-i-skip-items-in-a-legend#comment_544634
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/406-how-do-i-skip-items-in-a-legend#comment_579261
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/406-how-do-i-skip-items-in-a-legend#comment_579261
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/406-how-do-i-skip-items-in-a-legend#comment_650221
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/406-how-do-i-skip-items-in-a-legend#comment_650221
Sign in to comment.