MATLAB: Is there any way to put two plots in one legend entry

legend

is it possible to combine multiple plots in one legend entry as shown in the picture? . I tried various operations with [] and () but it didn't work.
Is there a way to put two plots in one legend entry?

Best Answer

That happens automatically unless you do one of the following:
  • call legend with a single legend entry, such as inside a loop or trying to place the legend call for each plot() right after the associated plot(); or call legend passing in only one graphics object handle
Some of the approaches that work are:
A)
for K = 1 : 6
this_x = something(K,:);
this_y = someother(K,:);
thislegend = sprintf('R%d', K); %construct a legend string


legends{K} = thislegend; %record it

plot(this_x, this_y);
hold on
end
legend(legends);
B)
for K = 1 : 6
this_x = something(K,:);
this_y = someother(K,:);
thislegend = sprintf('R%d', K); %construct a legend string
legends{K} = thislegend; %record it
linehandles(K) = plot(this_x, this_y);
hold on
end
legend(linehandles, legends);
C)
for K = 1 : 6
this_x = something(K,:);
this_y = someother(K,:);
thislegend = sprintf('R%d', K); %construct a legend string
linehandles(K) = plot(this_x, this_y, 'DisplayName', thislegend);
hold on
end
legend(linehandles, 'show');
D)
for K = 1 : 6
this_x = something(K,:);
this_y = someother(K,:);
plot(this_x, this_y);
hold on
end
legend({'R1', 'R2', 'R3', 'R4', 'R5', 'R6'});