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

108 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
plot(x,i*y)
hold on
end

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.
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:
plot(rand(10,5))
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:
% 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;
end
% Get the color
Col = ColOrd(ColRow,:);
% Plot the data
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]) % [1 1 1] == white
set(0,'DefaultAxesLineStyleOrder','-|:|*')
plot(rand(3))

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!