Reputation: 177
How can I restrict my legend in Matlab? So far I'm plotting a graph with code like so:
figure;
hold on;
plot(log(X),7.35,'ro',7.35,log(Y),'bo');
axis([7.3 7.7 7.3 7.7]);
set(gca,'xtick',[7:0.1:7.7])
set(gca,'ytick',[7:0.1:7.7])
title('atheism test file 1')
xlabel('x axis'); ylabel('y axis');
legend show;
Where X,Y are column vectors of 10 elements. When I do 'legend show'it will show 10 red circles data 1, data 2 etc. and then 10 blue circles. If I amend this to say legend('red','blue') It just gives me two red circles called red and blue...
What I want is to just take a legend to show 2 items, a red circle and a blue circle that I can name.
Upvotes: 2
Views: 718
Reputation: 32920
What you're plotting is actually 10 different points for log(X)
and 10 different points for log(Y)
, because for each plot the input vectors for each axis have different sizes. Simply make them to be of the same dimensions, like so:
plot(log(X), 7.35 * ones(size(X)), 'ro', 7.35 * ones(size(Y)), log(Y), 'bo');
and the legend will come out properly. There is no need for hold on
here (and no need to store any figure handles).
Upvotes: 3
Reputation: 1739
Plot red and blue separately, and then legend them explicitly. Try something like:
ph1 = plot(log(X),7.35,'ro');
ph2 = plot(7.35,log(Y),'bo');
legend([ph1(1), ph2(1)], 'red', 'blue');
Upvotes: 3