Automatically plot different colored lines
Asked 07 September, 2021
Viewed 1.4K times
  • 59
Votes

I'm trying to plot several kernel density estimations on the same graph, and I want them to all be different colors. I have a kludged solution using a string 'rgbcmyk' and stepping through it for each separate plot, but I start having duplicates after 7 iterations. Is there an easier/more efficient way to do this, and with more color options?

for n=1:10
 source(n).data=normrnd(rand()*100,abs(rand()*50),100,1); %generate random data
end
cstring='rgbcmyk'; % color string
figure
hold on
for n=1:length(source)
 [f,x]=ksdensity(source(n).data); % calculate the distribution
 plot(x,f,cstring(mod(n,7)+1))  % plot with a different color each time
end

5 Answer