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

The Plotting

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.

6 Answers 6

39

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.

2
  • 2
    used this one too since I use dynamically named curves (works great to plot a standard deviation but hiding them in legend).
    – gaborous
    Nov 6, 2014 at 0:37
  • 2
    Best solution if one wants to make this changes directly in the UI.
    – G. Führ
    Aug 2, 2015 at 18:16
27

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

3
  • 2
    Note that with this approach, once you turn off the legend and turn it back on via the UI, all the lines will be back in the legend.
    – Jonas
    Dec 3, 2012 at 20:59
  • 1
    PNG is better for this kind of image. Feb 2, 2013 at 1:10
  • 1
    SIde note: this is essentially what's recommended over at mathworks.com/help/matlab/creating_plots/… Nov 5, 2015 at 16:17
6

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.

1
  • 2
    I suggest setting the DisplayName property of the line handles, rather than call legend with the names, so that the result will be the same after toggling the legend off/on in the GUI.
    – Jonas
    Dec 3, 2012 at 21:01
1

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

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.

-2

Quick in-plot hack:

  1. Cut everything you don't want to appear in the legend
  2. Apply legend
  3. Paste
1
  • What do you mean by cut and paste? Do you mean move the other plot commands after the legend command? Provide a code snippet to demonstrate.
    – Cecilia
    Mar 10, 2017 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.