Control How Plotting Functions Select Colors and Line Styles
When you plot multiple data sets together in the same axes, MATLAB® automatically assigns different colors (and possibly line styles and markers) to the plot objects. You can customize the colors, line styles, and markers when you call a plotting function, and you can also set properties after calling the function.
For example, plot a solid red line and a dashed green line. Then add square markers to the red line and circular markers to the green line.
p1 = plot([0 1 2 3],'-r'); hold on p2 = plot([1 2 3 4],'--g'); hold off % Add markers p1.Marker = 'sq'; p2.Marker = 'o';
This approach is described in Specify Plot Colors. It is useful for customizing aspects of a few plots. However, it is less flexible in other situations, such as plotting data in a loop, or passing matrix data to plotting functions. In such cases, you can change the properties that control how MATLAB automatically assigns colors, line styles, and markers.
Note
Some of the functionality in the following examples is available starting in R2019b, and some of the functionality is available starting in R2020a. To modify plot colors and line styles in an earlier release, see Why Are Plot Lines Different Colors? and Line Styles Used for Plotting — LineStyleOrder.
How Automatic Assignment Works
MATLAB assigns colors to plot objects (such as Line
,
Scatter
, and Bar
objects) by cycling
through the colors listed in the ColorOrder
property
of the axes. The ColorOrder
property contains an array of RGB
triplets, where each RGB triplet defines a color. The default
ColorOrder
array contains seven colors. If you create more
objects than there are colors, the colors repeat.
If the plot objects support line styles and markers, MATLAB also cycles through the list in the LineStyleOrder
property of the axes. The LineStyleOrder
property contains a
cell array of character sequences, where each character sequence corresponds to a
line style (or a line style combined with a marker). The default
LineStyleOrder
array contains only the solid line style,
('-')
. All of the colors in the
ColorOrder
array are used with one character sequence in
the LineStyleOrder
array before the next sequence is used. The
cycle continues for each new plot object. If there are more objects than
combinations of colors and character sequences, then the cycle repeats.
For a given pair of ColorOrder
and
LineStyleOrder
arrays, the colors, line styles, and markers
for a particular plot object are determined by the value of the object's SeriesIndex
, which
is a new property starting in R2020a. By default, the
SeriesIndex
property is a number that corresponds to the
object's order of creation, starting at 1
. MATLAB uses the number to calculate indices into the
ColorOrder
and LineStyleOrder
arrays.
For example, create an axes object with two colors in its
ColorOrder
array (red and blue) and two line styles in its
LineStyleOrder
array (solid and dashed). Then plot five lines.
ax = axes; ax.ColorOrder = [1 0 0; 0 0 1]; ax.LineStyleOrder = {'-','--'}; hold on for i = 1:5 plot([i i+2]) end hold off
This table lists the SeriesIndex
, the index into the
ColorOrder
array, and the index into the
LineStyleOrder
array for each line in the preceding
plot.
SeriesIndex | Index into ColorOrder Array | Index into LineStyleOrder Array | Line Appearance | |
---|---|---|---|---|
First Line | 1 | 1 | 1 | Red solid line |
Second Line | 2 | 2 | 1 | Blue solid line |
Third Line | 3 | 1 | 2 | Red dashed line |
Fourth Line | 4 | 2 | 2 | Blue dashed line |
Fifth Line | 5 | 1 | 1 | Red solid line |
You can change the colors, line styles, and markers of plot objects by modifying
the ColorOrder
or LineStyleOrder
properties of the axes, or by changing the SeriesIndex
properties of the plot objects.
Changing Color Schemes and Line Styles
Changing the ColorOrder
property of the axes changes the
color scheme of your plot. Changing the LineStyleOrder
property
of the axes changes the line styles (and possibly markers) used in your plot. For
example, plot eight lines in a loop using the default colors and line style.
ax = axes; hold on for i = 0:7 plot([i i+2]) end hold off
Replace the ColorOrder
array with a new array that contains
four colors (you can also replace this array using the colororder
function). Then replace the
LineStyleOrder
array with a new cell array that contains
two line styles. The lines automatically use the new colors and line styles.
% Updates existing plots in R2019b or later ax.ColorOrder = [1 0 0; 0 1 0; 0 0 1; 0 0 0]; ax.LineStyleOrder = {'-','--'};
Changing Indices into the ColorOrder
and LineStyleOrder
Arrays
Changing the SeriesIndex
property on a plot object changes
the indices into the ColorOrder
and
LineStyleOrder
arrays. Changing the indices is useful when
you want the color, line style, and marker of an object to match another
object.
For example, plot four sine waves in a loop, varying the wavelength and phase. For
each sine wave, set the SeriesIndex
property according to the
wavelength. In the resulting plot, the sine waves that have the same wavelength also
have the same color.
x = linspace(0,10,200); ax = axes; hold on for phi = 0:3:3 for t = 1:2 plot(x,sin(x/t + phi),'SeriesIndex',t) % Requires R2020a or later end end hold off
To make one pair of sine waves more prominent, change the color order to different set of colors.
ax.ColorOrder = [0.8 0.8 0.9; 0.2 0.2 0.8];
See Also
Functions
plot
|gca
|colororder