Search code examples
matlabmatlab-figurelegend

Legend containing only specific plots


I have 10 curves in a plot, but only three of them should appear in the legend. For example, among 10 curves, just the first, 5th and 10th should be in the legend, how I can do this?

Here's my program:

x=1:0.5:15;
y1=x.^1
plot(x,y1)
hold on
y2=x.^1.2
plot(x,y2)
hold on
.
.
.
y10=x.^2.2
plot(x,y10)

Solution

  • You can use handles for the plots, and then specify the plots for the legend by their handles:

    x=1:0.5:15;
    y(1,:)=x.^1;
    y(2,:)=x.^1.2;
    ...
    ...
    ...
    y(10,:)=x.^2.2;
    
    for k=1:10
       h(k)=plot(x,y(k,:));
       hold on
    end
    legend([h(1) h(5) h(10)],'curve 1','curve 5','curve 10');
    hold off