Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Automatically plot different colored lines

Date: 2022-12-17 11:05:51

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

Answers: 

You could use a colormap such as HSV to generate a set of colors. For example: 

 

cc=hsv(12); figure; hold on; for i=1:12 plot([0 1],[0 i],'color',cc(i,:)); end

MATLAB has 13 different named colormaps ('doc colormap' lists them all).

Another option for plotting lines in different colors is to use the LineStyleOrder property; see Defining the Color of Lines for Plotting in the MATLAB documentation for more information.


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com