How to adjust legend

4 views (last 30 days)
Bas  Buddendorf
Bas Buddendorf on 4 Mar 2013
Hi,
I have a matrix M of which I have made a plot. However, the first 8 columns belong to a certain class (where Degree==1 and it uses only those columns for which this is true and does this for the whole matrix of 20 columns) I have given the columns belonging to the same class the same colour, now I want to have my legend displaying only those four classes (with a category name) instead of all 20 lines. Here is my code:
figure(1), clf('reset'),
plot(HMAX,R_time(:,Degree==1),'LineWidth',0.5,'Color','b');hold on
plot(HMAX,R_time(:,Degree==2),'LineWidth',0.5,'Color',[0 0.5 0]);
plot(HMAX,R_time(:,Degree==3),'LineWidth',0.5,'Color','r');
plot(HMAX,R_time(:,Degree==4),'LineWidth',0.5,'Color',[0 0.8 0.8]);
hold off
xlim([0.38 0.59]);
xlabel 'Hmax';
ylabel 'Time';
hget=get(gca,'title'); set(hget,'FontSize',20);
hget=get(gca,'xlabel'); set(hget,'FontSize',20);
hget=get(gca,'ylabel'); set(hget,'FontSize',20);
drawnow;
How can I can get a legend that only displays the four classes according to the colours I assigned?? Help is much appreciated!
Bas

Answers (1)

Juan Camilo Medina
Juan Camilo Medina on 5 Mar 2013
Edited: Juan Camilo Medina on 5 Mar 2013
"Legend" will label whatever has been drawn in your figure; therefore, you need to invoke the legend command right after you plotted whatever lines you want labeled, and before you plot the rest of the lines that you don't want labeled.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!