legend for group of lines

后端 未结 6 441
盖世英雄少女心
盖世英雄少女心 2020-12-08 07:46

I like to plot two groups of lines in the same plot. Each group has two lines with same color and I have to draw them in the order of one group after another group. I try to

6条回答
  •  甜味超标
    2020-12-08 08:30

    Actually, there is a non-hack way to do this, using hggroups. The below plots several lines, but the legend treats them as just two:

    t = 0:.1:2*pi;
    for k=1:5
        offset = k/7;
        m(:,k) = t+offset';
    end
    hSLines = plot(t,sin(m),'Color','b');hold on
    hCLines = plot(t,cos(m),'Color','g');
    hSGroup = hggroup;
    hCGroup = hggroup;
    set(hSLines,'Parent',hSGroup)
    set(hCLines,'Parent',hCGroup)
    % Include these hggroups in the legend:
    set(get(get(hSGroup,'Annotation'),'LegendInformation'),...
        'IconDisplayStyle','on'); 
    set(get(get(hCGroup,'Annotation'),'LegendInformation'),...
        'IconDisplayStyle','on'); 
    legend('Sine','Cosine')
    

    (shamelessly copied from http://www.mathworks.se/help/matlab/creating_plots/controlling-legends.html)

提交回复
热议问题