Generate Distinct Colors for Your MATLAB Plots
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!
Try GMPDC for yourself; you'll be modifying your own startup files before you know it! let us know what you think, or leave a comment for Tim here.
- Category:
- Picks
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.