Brett's Pick this week is "Generate Maximally Perceptually-Distinct Colors,", by Tim Holy.
Today, I'm going out on a limb: I'm going to Pick a new file from a new File Exchange author. As of this writing, "Generate Maximally-Perceptually Distinct Colors" (GMPDC)--the author's first File Exchange submission (welcome!)--has been downloaded only three times. (My download was the third.) However, I was excited to see this file, and I know it will come in handy. In fact, I've often considered writing something like this myself--but have never gotten around to it.
So what does GMPDC do? It provides an easy way to generate distinct, differentiable colors in which to plot a long sequence of objects (like lines).
MATLAB ships with several built-in colormaps that are useful in different situations. One of those, "lines," is a repeating array of seven distinct colors. By default, MATLAB axes cycle through these seven different colors when plotting lines:
% % You can see the default color order with: get(0,'DefaultAxesColorOrder') % and you can verify that they are the same as the repeating sequence in % "lines" with isequal(lines(7),get(0,'DefaultAxesColorOrder'))
ans = 0 0 1.0000 1.0000 0 0 0 1.0000 0 0 0 0.1724 1.0000 0.1034 0.7241 1.0000 0.8276 0 0 0.3448 0 ans = 1
Sometimes, however, I want to be able to differentiate more than 7 lines or plots. In the past, I've monkeyed about with custom color orders I made up on the fly. Tim makes it easy to standardize those colors; the colors returned by GMPDC are consistent regardless of the number of colors you request. As a bonus, one can specify a background color as an optional input to ensure that your colors differ from the background, as well as from each other:
Tim worked in the L*a*b* colorspace, which was designed to approximate human color perception. His code is solid and well implemented, with nice documentation. I hope this file is a harbinger of more good stuff to come from this author.
Note that the default "colororder" property can be readily modified. In fact, I just added a line to my startup file to set my colororder to be distinguishable_colors(20). Thanks, Tim!
Get the MATLAB code
Published with MATLAB® 7.11
Comments are closed.
7 CommentsOldest to Newest
[0 1 0; 1 0 1; 0 1 1; 1 0 0; .2 .6 1; 1 1 1; 1 .6 .2; 0 0 1; 1 .2 .6; .2 1 .6; .6 1 .2; .6 .2 1; 1 1 0]These were not determined scientifically as with Tim's method but was somewhat ad hoc and experimentally determined. So I would appreciate any comments on my choice and how appropriate it is for a black background. The plt utility extends this to up to 99 distinct colors using even more ad hoc decisions of increasingly questionable value as the number of colors increases so I don't know how it would stack up to Tim's more scientific approach. One drawback to the black background approach is that one must remember to invert the plot background color when printing (which plt does by default) otherwise you usually get a big mess. Also this means the colors which are optimized for black backgrounds may no longer be the best choice for the hardcopy. In fact yellow, which is a great choice on a black background generally is awful on the hardcopy because of the white background. (This is why yellow appears last in my sequence shown above). The other drawback to the black background is the shock value when the user first sees the results from my plt utility. Often they abandon the utility before getting used to this (and appreciating its benefits) or realizing that it can easily use the more conventional color schemes. ~Paul