How can I cycle through the 'ColorOrder' for my MATLAB figure when making multiple calls to my plot?

189 views (last 30 days)
I am continuously adding lines to my plot in a FOR loop and I would like each plot to use a different color and/or line style. I set the axes ColorOrder property to the colors that I would like the plot command to use. However, every line in my plot uses the first color defined in the ColorOrder, yellow, by default. I would like to force the plot command to cycle through all the colors defined in the ColorOrder property.
For example,
x = linspace(0,2*pi,50);
y = sin(x);
for i = 1:10
hold on

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 31 Aug 2010
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.
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;
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:
% Create some data
x = linspace(0,2*pi,50);
y = sin(x);
% Define your own Color Order Matrix
% For this example I used the default.
% It should be a 3-column matrix with entries in [0,1]
ColOrd = get(gca,'ColorOrder');
% Determine the number of colors in
% the matrix
[m,n] = size(ColOrd);
hold on
% Plot the data
for k = 1:10
% Determine which row to use in the
% Color Order Matrix
ColRow = rem(k,m);
if ColRow == 0
ColRow = m;
% Get the color
Col = ColOrd(ColRow,:);
% Plot the data
hold on
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])
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]) % [1 1 1] == white

More Answers (1)

Elmar Zander
Elmar Zander on 23 Jun 2014
There's also option 3: use
hold all
instead of
hold on
in your code.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by