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

橙三吉。 提交于 2019-11-27 18:29:42

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:

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.

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.

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

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.

KSmith

Quick in-plot hack:

  1. Cut everything you don't want to appear in the legend
  2. Apply legend
  3. Paste
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!