4.8 | 7 ratings Rate this file 122 Downloads (last 30 days) File Size: 3.36 KB File ID: #42673
image thumbnail

Beautiful and distinguishable line colors + colormap



18 Jul 2013 (Updated )

Plot lots of lines with very distinguishable and aesthetically pleasing colors. Works for 'N' colors

| Watch this File

File Information

The default Matlab default line spec and colormaps are astoundingly crude. This function creates distinguishable colors by spacing them out ~equally in *perceptive* space, not in RGB space. It is based off the research of professor Cynthia Brewer (colorbrewer!) but amazingly easy to use.

This function creates an Nx3 array of N [R B G] colors
These can be used to plot lots of lines with distinguishable and nice looking colors.

lineStyles = linspecer(N); makes N colors for you to use: lineStyles(ii,:)
colormap(linspecer); set your colormap to have easily distinguishable colors and a pleasing aesthetic
lineStyles = linspecer(N,'qualitative'); forces the colors to all be distinguishable (up to 12)
lineStyles = linspecer(N,'sequential'); forces the colors to vary along a spectrum


Examples demonstrating the colors.

X = linspace(0,pi*3,1000);
Y = bsxfun(@(x,n)sin(x+2*n*pi/N), X.', 1:N);
C = linspecer(N);
axes('NextPlot','replacechildren', 'ColorOrder',C);
ylim([-1.1 1.1]);

N = 6; X = linspace(0,pi*3,1000);
C = linspecer(N)
hold off;
for ii=1:N
    Y = sin(X+2*ii*pi/N);
    hold on;

A = rand(15);
figure; imagesc(A); % default colormap
figure; imagesc(A); colormap(linspecer); % linspecer colormap


Credits and where the function came from:

The colors are largely taken from:
http://colorbrewer2.org and Cynthia Brewer, Mark Harrower and The Pennsylvania State University
She studied this from a phsychometric perspective and crafted the colors

I made choices from the many there to decide the nicest once for plotting lines in Matlab. I also made a small change to one of the colors I thought was a bit too bright. In addition some interpolation is going on for the sequential line styles. An Apache-Style Software License is included in the file.


This file inspired Efficient 2 D Histogram, No Toolboxes Needed and Rgb2hex And Hex2rgb.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
28 May 2014 Anne Urai

beautiful, added this as the default colormap to my startup.m - no more hurting eyes. Thanks!

20 May 2014 G W

Great reference for colors after reading this article: http://eagereyes.org/basics/rainbow-color-map

20 May 2014 G W  
20 May 2014 G W  
29 Apr 2014 Andrew Tan

It's beautiful. Thank you!

03 Mar 2014 Jeremy Manning

What a beautiful colormap-- my new default. Thanks!

14 Aug 2013 lu li

Thanks a lot!

30 Jul 2013 Jonathan C. Lansey

Thanks for the most excellent feedback Stephen. I have updated the function to include almost all of your suggestions over the weekend. I hope it will be approved by Mathworks shortly.

28 Jul 2013 Stephen Cobeldick

Fix the above example: use of a colormap with multiline plots can be achieved by setting the 'ColorOrder' axes property:

N = 5;
axes('NextPlot','replacechildren', 'ColorOrder',cell2mat(linspecer(N)))
X = linspace(0,pi*3,1000);
Y = bsxfun(@(x,n)sin(x+2*n*pi/N), X.', 1:N);
plot(gca, X,Y, 'linewidth',5)

11. The M-file help section should include a 'See also ...' line, to link to similar and associated functions.
12. Distributing the colorbrewer scheme requires including a copy of the original Apache copyright license.

26 Jul 2013 Stephen Cobeldick

I have been looking for better looking, distinguishable colormaps to replace the standard ones, which at first this file seemed to offer. However this is not a drop in replacement colormap, and the M-file has multiple drawbacks, which detract from the idea:
1. Non-standard cell array output: all standard MATLAB colormaps return an Nx3 numeric matrix.
2. Non-standard default size of 255: the standard MATLAB colormaps use the current colormap size (very useful!).
3. No H1 line in the help.
4. Running mlint gives eight messages for code improvement.
5. The example does not work without the addition of extra code.
6. Calls slow functions like "exist" when "nargin" would suffice.
7. Unnecessary "for" loops could be vectorized (especially if returning the standard numeric colormap).
8. No array preallocation within the "for" loops.
9. Unnecessary use of "varargin" when a simple second input would be adequate.
10. Example shows poor use of MATLAB plot command. Compare to the code below, in half the number of lines:
N = 5;
X = linspace(0,pi*3,1000);
Y = bsxfun(@(x,n)sin(x+2*n*pi/N), X.', 1:N);

23 Jul 2013

I made the colors for higher 'N' a bit darker. Also made the demo a little cleaner.

01 Aug 2013

Adjusted the function based on the excellent suggestions of Stephen Cobeldick.

05 Aug 2013

small change to sequential output

Contact us