您需要使用命令hold on
來確保每次繪製新內容時繪圖都不會被擦除。
function test1
figure %# create a figure
hold on %# make sure the plot isn't overwritten
x=1:0.1:10;
%# if you want to use multiple colors
nPlots = 5; %# define n here so that you need to change it only once
color = hsv(nPlots); %# define a colormap
for k=1:nPlots; %# default step size is 1
y=k*sin(x);
plot(x,y,'Color',color(k,:));
end % /for-loop
end % /test1 - not necessary, btw.
編輯
你也可以做到這一點沒有一個循環,並繪製二維數組,建議由@Ofri:
function test1
figure
x = 1:0.1:10;
k = 1:5;
%# create the array to plot using a bit of linear algebra
plotData = sin(x)' * k; %'# every column is one k
plot(x,plotData)
工作!非常感謝你,天才 – Amit 2010-08-29 20:44:39
@Amit:很高興它工作得很好。我已經擴展了一下解決方案。 – Jonas 2010-08-29 22:22:54