File Exchange Pick of the Week

August 15th, 2008

Colors for Your Multi-Line Plots

Jiro's pick this week is VARYCOLOR by Daniel Helmick.

Have you ever had to plot many lines on a single graph and you had to construct additional set of colors to augment the 8 built-in colors? You may have to come up with a good set of colors that span a wide range of the spectrum. Daniel's varycolor will give you that color set with a single command.

Let's say I'm going to plot 50 lines. I can get the color matrix like this:

ColorSet = varycolor(50);

I can either index into this color matrix and use it as my Color property for plot:

    plot(rand(10,1), 'Color', ColorSet(1,:))

Or set it as the new ColorOrder property of an axes. Now, my subsequent plots will follow the new color order. (Be sure to use hold all to preserve the color order.)

set(gca, 'ColorOrder', ColorSet);

hold all;
for m = 1:50
  plot([0 51-m], [0 m]);
end

When I have this many lines, I often wonder what the best way to put a legend is. I can simply add the default legend:

legend show Location NorthEastOutside

But this is quite impractical. Instead, I can use the color matrix that I just created and use that in my color bar.

legend off
set(gcf, 'Colormap', ColorSet);
colorbar

Since I appropriately chose the number of colors to be the same as the number of lines, the colormap scale corresponds to the line number. Line 1 is green and line 40 is red.

As a bonus, take a look at Loren's blog on Plotting with Style to learn about other ways to customize the line style.

Comments

Tell us here how you customize your visualizations to convey your ideas as clearly as possible. Do you use colors, line styles, or markers? Or maybe some combinations.


Get the MATLAB code

Published with MATLAB® 7.6

2 Responses to “Colors for Your Multi-Line Plots”

  1. Paul Mennen replied on :

    > When I have this many lines, I often wonder what
    > the best way to put a legend is.

    Yes I agree that the usual Matlab legend is not practical with many traces (such as 50 as in your example). The color
    matrix is a clever alternative. However if you really need to identify the individual traces by name, that doesn’t help.

    There is yet another alternative for this problem that you may not be aware of. It is to use “plt” an alternative to matlab’s “plot” and “plotyy” that you will find on the file exchange at:

    http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=4936&objectType=file

    (or just search for “plt”.)

    If you run the demo program “demo\pltn.m” you will see a plot with 99 traces, each one with a unique name and a unique color!

    While identifying all 99 traces by color alone is a tall order, most people can distinguish at least the first 30 traces or so by color. One advantage of this legend style (besides being compact enough to be practical) is that if you can’t tell which trace is which based on color alone, just click on the legend entry and the trace will toggle, making it immediately obvious.

    Although you can tell plt to use Matlab’s default color scheme, or to use colors generated by VARYCOLOR, I’ve found that the problem of identifying more than about 10 traces using color is much easier when a black (or very dark) background is used for the plotting area. Although this is shocking at first, the advantages become more clear once you get used to it. (This is the default with plt). I think the reason for this is that you can maximize color saturation and plot contrast at the same time. The situation on paper (hardcopy) is reversed, I believe because of the distinction between emitted and reflected light.

    If you do get a chance to try out plt, I encourage you to run “demo\demoplt.m” and also to peruse the extensive documentation (plt.chm). If you have any questions or comments about it, I’m all ears.

    ~Paul

  2. Markus replied on :

    Hi Doug,

    long ago I fiddled out a number of distinguishable colors by hand. You find them in this matrix:

     
    colororder = [
    	0.00  0.00  1.00
    	0.00  0.50  0.00
    	1.00  0.00  0.00
    	0.00  0.75  0.75
    	0.75  0.00  0.75
    	0.75  0.75  0.00
    	0.25  0.25  0.25
    	0.75  0.25  0.25
    	0.95  0.95  0.00
    	0.25  0.25  0.75
    	0.75  0.75  0.75
    	0.00  1.00  0.00
    	0.76  0.57  0.17
    	0.54  0.63  0.22
    	0.34  0.57  0.92
    	1.00  0.10  0.60
    	0.88  0.75  0.73
    	0.10  0.49  0.47
    	0.66  0.34  0.65
    	0.99  0.41  0.23
    ];
    

    Since then, setting the default color order is in my Matlab startup file.

    Another good way to find distinguishable colors is to use the color spiral of James McNames: http://bsp.pdx.edu/Software/

    Yours
    Markus

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Doug: Ben, The name is in the name field as you saw in the video: files(1).name will give you just the name of the...
  • Ben: Hey, Thank you for the tutorial but I must ask, once you have the files required in the array, how do you parse...
  • jiro: Nisa, I’m not familiar with the Psychtoolbox. It’s not a toolbox developed by the MathWorks. As far...
  • nisa: Hi, I have just started to learn to use mathlab can this myaa codes be used with mathlab 7.5.0(R2007b)using...
  • Doug: @Rupa, Could you do this algorithm and then remove the two points and run again? Not efficient, but could be...
  • Doug: From the convhull doc http://www.mathworks .com/access/helpdesk /help/techdoc/ref/co nvhull.html xx = -1:.05:1;...
  • Mark: Very nice function, indeed, thanks for that. But why does’t MATLAB freeze the colormaps for subplots...
  • Steve L: MAuricio, From the description it sounds like the features variable is a cell array that itself contains a...
  • Fred: Doug, I have a set of coordinates for the perimeter of an object in random order. I want to plot these using...
  • Rupa: Hi, sorry to digress but I have a similar problem, I think. I would like to find the three (or n) smallest...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.