Documentation Center

  • Trials
  • Product Updates

Defining the Color of Lines for Plotting


The axes ColorOrder property determines the color of the individual lines drawn by the plot and plot3 functions. For multiline graphs, these functions cycle through the colors defined by ColorOrder, repeating the cycle when they reach the end of the list.

The colordef command defines various color order schemes for different background colors. colordef is typically called in the matlabrc file, which is executed during the MATLAB® software startup.

Defining Your Own ColorOrder

You can redefine ColorOrder to be any m-by-3 matrix of RGB values, where m is the number of colors. However, high-level functions like plot and plot3 reset most axes properties (including ColorOrder) to the defaults each time you call them. To use your own ColorOrder definition you must do one of the following three things:

  • Define a default ColorOrder on the figure or root level

  • Change the axes NextPlot property to add or replacechildren

  • Use the informal form of the line function, which obeys the ColorOrder but does not clear the axes or reset properties

Changing the Default ColorOrder

You can define a new ColorOrder that MATLAB uses within a particular figure, for all axes within any figures created during the MATLAB session, or as a user-defined default that MATLAB always uses.

To change the ColorOrder for all plots in the current figure, set a default in that figure. For example, to set ColorOrder to the colors red, green, and blue, use the statement

set(gcf,'DefaultAxesColorOrder',[1 0 0;0 1 0;0 0 1])

To define a new ColorOrder that MATLAB uses for all plotting during your entire MATLAB session, set a default on the root level so axes created in any figure use your defaults.

set(0,'DefaultAxesColorOrder',[1 0 0;0 1 0;0 0 1])

To define a new ColorOrder that MATLAB always uses, place the previous statement in your startup.m file.

Setting the NextPlot Property

The axes NextPlot property determines how high-level graphics functions draw into an existing axes. You can use this property to prevent plot and plot3 from resetting the ColorOrder property each time you call them, but still clear the axes of any existing plots.

By default, NextPlot is set to replace, which is equivalent to a cla reset command (i.e., delete all axes children and reset all properties, except Position, to their defaults). If you set NextPlot to replacechildren,


MATLAB deletes the axes children, but does not reset axes properties. This is equivalent to a cla command without the reset.

After setting NextPlot to replacechildren, you can redefine the ColorOrder property and call plot and plot3 without affecting the ColorOrder.

Setting NextPlot to add is the equivalent of issuing the hold on command. This setting prevents MATLAB from resetting the ColorOrder property, but it does not clear the axes children with each call to a plotting function.

Using the line Function

The behavior of the line function depends on its calling syntax. When you use the informal form (which does not include any explicit property definitions),


line obeys the ColorOrder property, but does not clear the axes with each invocation or change the view to 3-D (as plot3 does). However, line can be useful for creating your own plotting functions where you do not want the automatic behavior of plot or plot3, but you do want multiline graphs to use a particular ColorOrder.

Line Styles Used for Plotting — LineStyleOrder

The axes LineStyleOrder property is analogous to the ColorOrder property. It specifies the line styles to use for multiline plots created with the plot and plot3 functions. MATLAB increments the line style only after using all of the colors in the ColorOrder property. It then uses all the colors again with the second line style, and so on.

For example, define a default ColorOrder of red, green, and blue and a default LineStyleOrder of solid, dashed, and dotted lines.

set(0,'DefaultAxesColorOrder',[1 0 0;0 1 0;0 0 1],...

Then plot some multiline data.

t = 0:pi/20:2*pi;
a = ones(length(t),9);
for i = 1:9
    a(:,i) = sin(t-i/5)';

MATLAB cycles through all colors for each line style.

Was this topic helpful?