7
votes

When I plot several curves on the same plot using hold on, each curve defaults to the same color (blue). I'd like them to have all different colors.

One solution I've seen is to make a color vector, e.g. c = ['k', 'g', 'r', ...] and loop over it, but I don't like this solution. Things will break if my number of plots is greater than the length of my color vector c, and I don't want to have to define c in every file.

Is there a better solution?

2

2 Answers

11
votes

Try using hold all instead. Your curves should cycle over the colormap automatically. From help hold:

hold all holds the graph and the current line color and line style so that subsequent plotting commands do not reset the ColorOrder and LineStyleOrder property values to the beginning of the list. Plotting commands continue cycling through the predefined colors and line styles from where the last graph stopped in the list.

You can examine the colormap with get(gca,'ColorOrder').

3
votes

You can also change the default 'ColorOrder' property on the global environment doing:

set(0,'DefaultAxesColorOrder',hsv(10))

where I replaced the default colors for a set of 10 colors given by hsv colormap. Of course, you can also place any other set of colors using either one the colormap color generators (hsv,hot,cooper,lines, ..., use doc colormap for more details ), as I did before, or just placing any n x 3 matrix. In case you want to use the list of colors: {black, green, red} for the current axis you would do:

set( gca,'ColorOrder', [0 0 0; 1 0 0; 0 1 0] )