How to show legend for only a specific subset of c

2019-01-13 14:44发布

问题:

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', '');

There are several curves in my plotting. I want to display legend for only some of them. How do I do it?

For example, how do I make only the legend for the cosine curve visible in the plotting above? When I call the legend() functions as legend('', 'cosine'); instead of adding the empty third parameter, indeed the third green line is removed from the legend. But that doesn't solve my problem, because the undesired red line stays visible.

回答1:

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:



回答2:

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.



回答3:

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:

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.



回答4:

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')


回答5:

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.



回答6:

Quick in-plot hack:

  1. Cut everything you don't want to appear in the legend
  2. Apply legend
  3. Paste