Graphics Previous page   Next Page

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 MATLAB 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:

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

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.

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.


Previous page  Example -- Simulating Multiple Colormaps in a Figure Line Styles Used for Plotting -- LineStyleOrder Next page

© 1994-2005 The MathWorks, Inc.