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

后端 未结 6 1071
谎友^
谎友^ 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')
    
    0 讨论(0)
  • 2020-12-04 22:24

    To expand Sebastian's answer, I have a special case where I'm plotting several lines in one of two formats (truss beams either in compression or tension) and was able to plot specific plot handles in the legend as long as the labels were the same length

    for ii=1:nBeams
        if X(ii)<0 %Bars with negative force are in compession
            h1=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
                linspace(beamCord(ii,2),beamCord(ii,4)),'r:');
        elseif X(ii)>0 %Bars with positive force are in tension
            h2=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
                linspace(beamCord(ii,2),beamCord(ii,4)),'b');
        end
    end
    
    legend([h1;h2],['Compression';'Tension    ']);
    

    Where 4 spaces have been added behind 'Tension' so that the number of characters is consistent.

    0 讨论(0)
  • 2020-12-04 22:26

    Let's start with your variables and plot them:

    t = 0 : 0.01 : 2 * pi;
    s = sin(t);
    c = cos(t);
    m = -sin(t);
    
    figure;
    hold ('all');
    hs = plot(t, s);
    hc = plot(t, c);
    hm = plot(t, m);
    

    There is a property called IconDisplayStyle. It is buried quite deep. The path you need to follow is:

    Line -> Annotation -> LegendInformation -> IconDisplayStyle

    Setting the IconDisplayStyle property off will let you skip that line. As an example, I am going to turn off hs's legend.

    hsAnno = get(hs, 'Annotation');
    hsLegend = get(hsAnno, 'LegendInformation');
    set(hsLegend, 'IconDisplayStyle', 'off');
    

    Of course you can go ahead and do it like this:

    set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
    

    But I find it much harder to understand.

    Now, the legend function will just skip hs.

    Ending my code with this:

    legend('cosine', 'repeat for this handle')
    

    will give you this: enter image description here

    EDIT: Jonas had a nice suggestion in the comments: Setting the DisplayName property of hc like this:

    set(hc, 'DisplayName', 'cosine');
    legend(gca, 'show');
    

    will give you the legend you need. You will have associated your line handle with 'cosine'. So, you can just call the legend with 'off' or 'show' parameters.

    0 讨论(0)
  • 2020-12-04 22:28

    Quick in-plot hack:

    1. Cut everything you don't want to appear in the legend
    2. Apply legend
    3. Paste
    0 讨论(0)
  • 2020-12-04 22:31

    Just store the desired legend handles in a variable and pass the array to legend. In your case, it would only be one value, like so:

    hold on;
    plot(t, s, 'r');
    h2 = plot(t, c, 'b');  % # Storing only the desired handle
    plot(t, m, 'g');
    hold off;
    
    legend(h2, 'cosine');  % # Passing only the desired handle
    

    You should get this plot:

    enter image description here

    0 讨论(0)
  • 2020-12-04 22:32

    I do not like storing the handle values, it becomes a mess when I have a lot of graphs in my figures. Therefore i found another solution.

    t = 0 : 0.01 : 2 * pi;
    s = sin(t);
    c = cos(t);
    m = -sin(t);
    hold on;
    plot(t, s, 'r', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
    h2 = plot(t, c, 'b', 'DisplayName', 'cosine');  % Plotting and giving legend name
    plot(t, m, 'g', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
    
    legend show  % Generating legend based on already submitted values
    

    This give me the same graph as shown in Eitan T's answer.

    It should be noted that this will affect other matlab functions also, for example will cla only remove the plots mentioned on the legend. Search for HandleVisibility in the Matlab documentation for more about that.

    0 讨论(0)
提交回复
热议问题