Matlab – Cycle over colors while plotting in MATLAB

colorsMATLABplot

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?

Best Answer

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').

Related Topic