Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically plot different colored lines

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 
like image 304
Doresoom Avatar asked Jan 08 '10 16:01

Doresoom


People also ask

How do I change the line color in a plot?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument. See also the plot command's documentation.

How do you get different colors in Matlab?

Specify Color of a Bar Chart Now, change the bar fill color and outline color to light blue by setting the FaceColor and EdgeColor properties to the hexadecimal color code, '#80B3FF' . Before R2019a, specify an RGB triplet instead of a hexadecimal color code. For example, b. FaceColor = [0.5 0.7 1] .

How do you set a color plot in Matlab?

For a custom color, specify an RGB triplet or a hexadecimal color code. An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1] ; for example, [0.4 0.6 0.7] .


2 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.

like image 131
Azim J Avatar answered Sep 21 '22 18:09

Azim J


Actually, a decent shortcut method for getting the colors to cycle is to use hold all; in place of hold on;. Each successive plot will rotate (automatically for you) through MATLAB's default colormap.

From the MATLAB site on hold:

hold all holds the plot and the current line color and line style so that subsequent plotting commands do not reset the ColorOrder and LineStyleOrder property values to the beginning of the list. Plotting commands continue cycling through the predefined colors and linestyles from where the last plot stopped in the list.

like image 23
Mark Elliot Avatar answered Sep 18 '22 18:09

Mark Elliot