How do I create a legend describing a subset of the lines in my axes?

19 ビュー (過去 30 日間)
How do I create a legend describing a subset of the lines in my axes? Is it possible to select which lines are shown in the legend?
For example, if I have a plot with 15 lines, how do I create a legend describing only 3 of those lines?

採用された回答

MathWorks Support Team
MathWorks Support Team 2018 年 9 月 6 日
There is an option for the LEGEND command that allows you to specify the handles of the objects that should be included in your legend. The following code can be studied as an example:
% create some arbitrary data
% the data will be scales of sine, cosine, and tangent curves
t = (0:.1:2*pi)';
x = sin(t);
sc = linspace(.8,1.2,10); % scaling matrix
x1 = repmat(x,1,10);
x1 = x1.*repmat(sc,length(t),1);
x = cos(t);
x2 = repmat(x,1,10);
x2 = x2.*repmat(sc,length(t),1);
x = tan(t);
x3 = repmat(x,1,10);
x3 = x3.*repmat(sc,length(t),1);
% plot the data
hf = figure;
hsin = plot(t,x1,'r');
hold on
hcos = plot(t,x2,'g');
htan = plot(t,x3,'b');
axis([0 2*pi -2 2])
% extract the handles that require legend entries
hleglines = [hsin(1) hcos(1) htan(1)];
% create the legend
hleg = legend(hleglines,'sine curves','cosine curves','tangent curves');
For more information on Handle Graphics and using GET and SET, please see the following resources:
Handle Graphics Object Properties

その他の回答 (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