Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

legend for selected plot objects in MATLAB figure

For a MATLAB figure, I have something like:

figure; hold on;
line ( [1 2], [3 4] );
line ( [5 6], [7 8] );

plot(x1,y1,'r.');
plot(x2,y2,'b.');

where x1,y1,x2,y2 are all vectors.

How can I add legend for only the last two plots, not for the two lines?

like image 609
Chang Avatar asked Jun 30 '26 04:06

Chang


1 Answers

You will have to get the handles for the last two plots and tell legend to plot only for those two. For example:

h1 = plot(x1,y1,'r.');
h2 = plot(x2,y2,'b.');
legend([h1,h2],'red','blue')
like image 138
abcd Avatar answered Jul 03 '26 11:07

abcd