Reputation: 3884
I plot with different data sets and all plots are on the same axes. The issue I am facing here is adding the legend and when i plot the next plot the legend of the first gets overwritten our overlapped. How can I have the legends of consecutive plots below the other and not over
Thanks
Upvotes: 1
Views: 508
Reputation: 38042
The thing with legends
is that it creates a whole new legend whenever you call the command. You should therefore only draw legends once.
Here's an INCORRECT way to do it:
% THIS IS NOT CORRECT
plot(x1, y1, 'r.'); legend('first plot')
plot(x2, y2, 'g.'); legend('second plot')
plot(x3, y3, 'b.'); legend('third plot')
plot(x4, y4, 'k.'); legend('fourth plot')
which will create four overlapping legends. The RIGHT way to do it would be
plot(x1, y1, 'r.');
plot(x2, y2, 'g.');
plot(x3, y3, 'b.');
plot(x4, y4, 'k.');
% only 1 call to legend
legend('first plot', 'second plot', 'third plot', 'fourth plot')
or, alternatively, to keep plot and legend entry together,
plot(x1, y1, 'r.'); L{1} = 'first plot';
plot(x2, y2, 'g.'); L{2} = 'second plot';
plot(x3, y3, 'b.'); L{3} = 'third plot';
plot(x4, y4, 'k.'); L{4} = 'fourth plot';
legend(L{:});
Upvotes: 2