Help with plotting multiple line complete with legends.

조회 수: 194 (최근 30일)
Tim Mottram
Tim Mottram 2012년 3월 7일
Hi there, This question has a few parts so bare with me if it seems confused, I'll try my best to be clear. My problem is this, I want to plot 11 lines in different colours on the same graph complete with legends. As you all know I should be able plot with the command -- plot(x,y,'blue',x,z'red......). I run out of colours with specific names and therefore have to define them with [rand1,rand2,rand3]. To use a color like this I have to use 'color' before using the 1:3 color matrix with the plot command. So now I have - plot(x,y,blue,.....x,n,'color',[rand1,rand2,rand3]) This turns all the lines the same colour as [rand1,rand2,rand3] ! In addition to this I don't really like having to write one massive plot command, I would prefer to do it in a for loop, In this loop I would like to be able to add legends as I go. But only one legend is ever added! So if anyone has a solution to my problem (Maybe making some sort of color map if there isn't anything simpler..) I would be very much in your debt. Thanks in advance. Tim.

채택된 답변

Dr. Seis
Dr. Seis 2012년 3월 7일
Something like this...
x = 1 : 50;
y = rand(11,50); % 11 traces, 50 samples long
h = zeros(11,1); % initialize handles for 11 plots
figure;
h(1)=plot(x,y(1,:),'color',[rand(1),rand(1),rand(1)]); hold on;
for ii = 2 : 11
h(ii)=plot(x,y(ii,:),'color',[rand(1),rand(1),rand(1)]);
end
hold off;
legend(h,'plot1','plot2','plot3','plot4','plot5','plot6','plot7',...
'plot8','plot9','plot10','plot11');
  댓글 수: 3
Dr. Seis
Dr. Seis 2012년 3월 7일
Kevin's post is also good if you know you are plotting N lines and want to have a legend entry for each line. I like using handles especially when, for example, I have M different groups of N lines... so I only want to have a legend entry for each M group instead of a legend entry for M*N lines.
Tim Mottram
Tim Mottram 2012년 3월 7일
Just checked, this works as described.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kevin Holst
Kevin Holst 2012년 3월 7일
Tim,
See a previous answer that I helped a guy out with here:
Also, 11 different colors will be fairly jumbled on a plot, in my opinion. Have you thought of using 4 colors and 3 line styles?

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by