Late answer, but two things to add:
- For information on how to change the
'ColorOrder'
property and how to set a global default with 'DefaultAxesColorOrder'
, see the "Appendix" at the bottom of this post.
- There is a great tool on the MATLAB Central File Exchange to generate any number of visually distinct colors, if you have the Image Processing Toolbox to use it. Read on for details.
The ColorOrder
axes
property allows MATLAB to automatically cycle through a list of colors when using hold on/all
(again, see Appendix below for how to set
/get
the ColorOrder
for a specific axis or globally via DefaultAxesColorOrder
). However, by default MATLAB only specifies a short list of colors (just 7 as of R2013b) to cycle through, and on the other hand it can be problematic to find a good set of colors for more data series. For 10 plots, you obviously cannot rely on the default ColorOrder
.
A great way to define N visually distinct colors is with the "Generate Maximally Perceptually-Distinct Colors" (GMPDC) submission on the MATLAB Central File File Exchange. It is best described in the author's own words:
This function generates a set of colors which are distinguishable by reference to the "Lab" color space, which more closely matches human color perception than RGB. Given an initial large list of possible colors, it iteratively chooses the entry in the list that is farthest (in Lab space) from all previously-chosen entries.
For example, when 25 colors are requested:
The GMPDC submission was chosen on MathWorks' official blog as Pick of the Week in 2010 in part because of the ability to request an arbitrary number of colors (in contrast to MATLAB's built in 7 default colors). They even made the excellent suggestion to set MATLAB's ColorOrder
on startup to,
distinguishable_colors(20)
Of course, you can set the ColorOrder
for a single axis or simply generate a list of colors to use in any way you like. For example, to generate 10 "maximally perceptually-distinct colors" and use them for 10 plots on the same axis (but not using ColorOrder
, thus requiring a loop):
% Starting with X of size N-by-P-by-2, where P is number of plots
mpdc10 = distinguishable_colors(10) % 10x3 color list
hold on
for ii=1:size(X,2),
plot(X(:,ii,1),X(:,ii,2),'.','Color',mpdc10(ii,:));
end
The process is simplified, requiring no for
loop, with the ColorOrder
axis property:
% X of size N-by-P-by-2
mpdc10 = distinguishable_colors(10)
ha = axes; hold(ha,'on')
set(ha,'ColorOrder',mpdc10) % --- set ColorOrder HERE ---
plot(X(:,:,1),X(:,:,2),'-.') % loop NOT needed, 'Color' NOT needed. Yay!
APPENDIX
To get the ColorOrder
RGB array used for the current axis,
get(gca,'ColorOrder')
To get the default ColorOrder
for new axes,
get(0,'DefaultAxesColorOrder')
Example of setting new global ColorOrder
with 10 colors on MATLAB start, in startup.m
:
set(0,'DefaultAxesColorOrder',distinguishable_colors(10))
hold on
is functionally equivalent tohold all
. However, the question of how to get more than the 7 default colors remains. A default color map may be a solution as described by Azim or a function to generate colors tuned for easy visual discrimination can be used, as below. – Palpablehold all
I get plots in different colors (even though I must say they look pretty dull), whereas when I usehold on
, all my plots become blue. So I wonder what functional equivalence is. – Mammyhold on
merely prevents MATLAB from erasing the previous plots when making a newplot
call.hold all
assigns new attributes to subsequent calls to plot as well. – Divorceehold on
gives different color plots. You're using and older version. From the current docs, "hold all
is the same ashold on
. Note: This syntax will be removed in a future release. Usehold on
instead." See also the release notes. – Palpablegraphicsversion('handlegraphics')
returnstrue
, indicating MATLAB is running with the old handle graphics system. – Palpable