Why Are Plot Lines Different Colors?

New Color Order

Starting in R2014b, MATLAB® graphics has a new color order that determines the colors used in plots. This table shows the color order introduced in R2014b versus previous releases. It also lists the RGB triplet values that define the colors.

Starting in R2014bR2014a and Earlier

         0    0.4470    0.7410
    0.8500    0.3250    0.0980
    0.9290    0.6940    0.1250
    0.4940    0.1840    0.5560
    0.4660    0.6740    0.1880
    0.3010    0.7450    0.9330
    0.6350    0.0780    0.1840
         0         0    1.0000
         0    0.5000         0
    1.0000         0         0
         0    0.7500    0.7500
    0.7500         0    0.7500
    0.7500    0.7500         0
    0.2500    0.2500    0.2500

The ColorOrder property of the axes contains the color order. To change the color order, set a different default value for the ColorOrder property. For example, this code sets the default color order to the colors used in previous releases.

co = [0 0 1;
      0 0.5 0;
      1 0 0;
      0 0.75 0.75;
      0.75 0 0.75;
      0.75 0.75 0;
      0.25 0.25 0.25];
set(groot,'defaultAxesColorOrder',co)

The hold Command Cycles Through Colors

In R2014a and earlier, the hold on command does not retain the current color so new plots added to the axes start from the beginning of the color order. Visually, this means that new plots use the same initial color. Starting in R2014b, the hold on command retains the current color so that new plots added to the axes use the next colors in the color order.

For example, this code displays six lines using the hold on command. Starting in R2014b, the lines cycle through the color order and the resulting plot uses the first six colors of the color order.

data = [5 6 7; 2 3 4; 4 5 6];
plot(data);
hold on
plot(3*data);
hold off

If you run the same code in previous releases, the color order restarts with each plotting command. The resulting plot uses the first three colors of the color order twice.

Restart Color Order

Starting in R2014b, if you want to restart the color order before each plotting command, then set the ColorOrderIndex property of the axes to 1.

data = [5 6 7; 2 3 4; 4 5 6];
plot(data);
hold on
ax = gca;
ax.ColorOrderIndex = 1;
plot(3*data);
hold off

See Also