How to show legend for only a specific subset of curves in the plotting?

后端 未结 6 1077
谎友^
谎友^ 2020-12-04 22:11
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);

hold on;
plot(t, s, \'r\');
plot(t, c, \'b\');
plot(t, m, \'g\');
hold off;

legend(\'\', \'cosine\', \'         


        
6条回答
  •  庸人自扰
    2020-12-04 22:23

    You could just change the order in wich the curves are plotted and apply the legend to the first curve:

    t = 0 : 0.01 : 2 * pi;
    s = sin(t);
    c = cos(t);
    m = -sin(t);
    
    plot(t,c,t,s,t,m)  % cosine is plotted FIRST
    legend('cosine')   % legend for the FIRST element
    

    if i'd want to put in a legend for cosine and -sine:

    plot(t,c,t,m,t,s)  % cosine and -sine are first and second curves
    legend('cosine', '-sine')
    

提交回复
热议问题