File Exchange Pick of the Week

Our best user submissions

Colors for Your Multi-Line Plots 14

Posted by Jiro Doke,

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]);
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);
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


Comments are closed.

14 CommentsOldest to Newest

Paul Mennen replied on : 1 of 14
> 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: (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
Markus replied on : 2 of 14
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: Yours Markus
Geoffrey replied on : 4 of 14
This coding could help me but when I choose 8 lines and then I try to insert a legend I get 50 data lines with the 8 colors repeating. How can I get a legend with just my 8 lines. I don't know if you have ran into this problem. thanks
Scott Otterson replied on : 5 of 14
Markus: Thanks! I'm using this. Geoffrey: I haven't seen your code but my guess is that you're setting the color order and then calling plot(). The problem with this is that plot() resets the ColorOrder property. You can prevent that by preceding plot() with a command that freezes ColorOrder. Either like this:
  set(gca, 'ColorOrder', colororder);
  hold on
or like this:
  set(gca, 'ColorOrder', colororder);
naga replied on : 6 of 14
hi i am using like this please help me: i have a cell array z{k}, i want to plot it with different colours, i tried ur code, but its not working please help me. ColorSet = varycolor(280); set(gca, 'ColorOrder', ColorSet); plot(z{k}) hold on when i tried this, i am getting error like this ??? Undefined function or method 'varycolor' for input arguments of type 'double'.
jiro replied on : 7 of 14
@naga, Did you download “varycolor” from the File Exchange? Also, make sure that you put the function at a location where MATLAB can find it.
Jacob replied on : 9 of 14
Hello, How do you call out the colors from the color map and apply them to individual plots? For example, say I wanted to use color 10 from the colorbar generated from the code below. How could I put it in my plot command? Is it done just like calling out the default colors? I know I could just use one of the default colors but I have 12 plots that require different colors for my project. NumberOfPlots=12; ColorSet=varycolor(NumberOfPlots); figure hold on; for m=1:NumberOfPlots plot(ones(20,1)*m,'Color',ColorSet(m,:)) end set(gcf, 'Colormap', ColorSet); colorbar plot(x,y,'?')
Usman Azeem replied on : 11 of 14
Chval da Bacha !!!! Banda Profile pict hee koi kaam ki lagaa deta hai... Ddeengi boothi wala... !!!!
Jiro Doke replied on : 13 of 14
@Christine, The function "lines" give you the default MATLAB line colors. In the above example, if you type ColorSet = lines(50); it will give you the default colors. However, "lines" only give you 7 distinct colors, and it simply repeats itself after that.
Udi replied on : 14 of 14
I'm trying this on a series of ~2600 lines and get black colorbar :( Matlab 2011b. My code (relevant parts): while j<length(T)-1 if mod(j+1,1000)==0 ... if j+1==1000 set(gca,'fontsize',24, 'ColorOrder', plat); hold all; ... end plot(X,u(2,:),'linewidth',2); if j+1==length(plat(:,1)) set(gca, 'Colormap', plat); colorbar('ColorOrder',plat) end end j=j+1; ... end