The ColorOrder property is only respected when plotting the entire data with all sequences calling the PLOT command only once.
If you must use a FOR loop to call the PLOT command, each successive call of the PLOT command is treated independently and will only consider the first value of the ColorOrder sequence.
To resolve this, you can consider (1) vectorizing your code such that the PLOT command is called only once or (2) use a FOR loop construct by creating your own color order sequence and manually specifying the color marker in each PLOT command within the loop. These two options will be discussed next.
OPTION (1):
The behavior you are experiencing is the result of calling the plot command multiple times. When the plot command is called, it always starts with the first color defined in the axes ColorOrder property. Therefore, if you are only plotting one line at a time, it will always use the same color - the first color in the ColorOrder.
The plot command cycles through colors when more than one line is plotted at a time. For example:
This will plot five lines, one line for each column. If you are using the default ColorOrder, then each line is a unique color.
If it is possible to vectorize your code, then the plot command will automatically cycle through colors. For example:
x = linspace(0,2*pi,50);
y = sin(x);
i = 1:10;
X = x' * ones(size(i));
Y = y' * i;
plot(X,Y)
OPTION (2):
If, however, the number of points in your data changes during each iteration through the loop, then you will have to define your own color matrix and index into this matrix to determine which color to use for the plot. Below is an example of how to do this:
x = linspace(0,2*pi,50);
y = sin(x);
ColOrd = get(gca,'ColorOrder');
[m,n] = size(ColOrd);
hold on
for k = 1:10
ColRow = rem(k,m);
if ColRow == 0
ColRow = m;
end
Col = ColOrd(ColRow,:);
plot(x,k*y,'Color',Col)
hold on
end
hold off
axis normal
When both the 'ColorOrder' and 'LineStyleOrder' are set, the line style is the major attribute, and color is the minor. This means that MATLAB will cycle through all the colors before changing to another line style.
For example:
set(0,'DefaultAxesColorOrder',[1 0 0;0 0 1])
set(0,'DefaultAxesLineStyleOrder','-|*')
plot(rand(4))
This will produce 4 lines. Below is a table which illustrates the color and line style for each line:
LINE Line Style Color
1 - [1 0 0]
2 - [0 0 1]
3 * [1 0 0]
4 * [0 0 1]
The line style is fixed while MATLAB cycles through the colors. Once all the colors are used, MATLAB moves on to the next line style and repeats the procedure until all the lines are drawn.
If MATLAB runs out of colors and line styles, MATLAB starts with the first color and first line style again and repeats the same procedure until all lines are drawn.
If you would like to cycle through line styles and not colors, you must first set the axes ColorOrder property to a singles color. Next, set the axes LineStyleOrder property to the desired line styles. For example:
set(0,'DefaultAxesColorOrder',[1 1 1])
set(0,'DefaultAxesLineStyleOrder','-|:|*')
plot(rand(3))
Best Answer