Help with plotting multiple line complete with legends.

142 ビュー (過去 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 Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by