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
If this one actually does as advertised, it is on the fast-track onto my Matlab path.
Now, if only there was a way to select colors from this palette using a shorthand syntax similar to plot ( x, y, ‘r.-‘ ) where the ‘r’ is changed to some form of index…..
you can access the colors from the new ColorOrder as in ‘option 2’ in this link
‘r’,’g’ and so on should still be accessible when changing the ColorOrder though.
I tried to do this once the other way around – by starting out sampling L*a*b* space, and converting uniform points back to sRGB. The problem with that is that many of the points were out of gamut, so you had to worry about gamut mapping — the color didn’t exist in sRGB color.
Nice – except that it requires the image processing toolbox. How about a version for the rest of us who don’t do image processing but do plot several lines on a single axis.
Whoops. Guess I should have read the entire posting before complaining about the image processing toolbox requirement. Downloaded the colorspace entry and everything works great. Already in my startup.
I find this quite interesting since I have often tried to distinguish many traces on a plot on the bases of color alone. One thing I’ve noticed is that matlab’s default color order avoids using some fully saturated colors. I suspect this is because those colors lead to low contrast when plotted against the usual white background. This lead me to experiment with plots with black backgrounds which I found allowed the use of higher color saturation and made it easier to distinguish more traces by their color. I was wondering if Tim or others who have studied the complex human color response could confirm or deny this conjecture.
My plt.m plotting tool (on the FEX) can of course use matlab’s default colors (or the colors from Tim’s GMPDC) but it’s default scheme if none is specified is to use a black background with the following color choices for the first 13 traces:
[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.