File Exchange

## Beautiful and distinguishable line colors + colormap

version 1.4.0.0 (8.25 KB) by Jonathan C. Lansey

### Jonathan C. Lansey (view profile)

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

Updated 16 Sep 2015

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.

% LINE COLORS
N=6;
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);
plot(X,Y,'linewidth',5)
ylim([-1.1 1.1]);

% SIMPLER LINE COLOR EXAMPLE
N = 6; X = linspace(0,pi*3,1000);
C = linspecer(N)
hold off;
for ii=1:N
Y = sin(X+2*ii*pi/N);
plot(X,Y,'color',C(ii,:),'linewidth',3);
hold on;
end

% COLORMAP EXAMPLE
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
beautifully.

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.

### Cite As

Jonathan C. Lansey (2020). Beautiful and distinguishable line colors + colormap (https://www.mathworks.com/matlabcentral/fileexchange/42673-beautiful-and-distinguishable-line-colors-colormap), MATLAB Central File Exchange. Retrieved .

qiu tao

yukun zhang

### yukun zhang (view profile)

thanks for your code, it really helps me to improve my pic in my paper job

Svetlana Baranova

### Svetlana Baranova (view profile)

Thank you for nice function!

Jakob Jongsma

Great!

MaRu

Xiangjie Xie

YH B

Lander Vanroye

### Lander Vanroye (view profile)

Ajey Venkataraman

ZhiH

### ZhiH (view profile)

Thank you for sharing this file!

Hanghang Ma

Chen Yingqi

### Chen Yingqi (view profile)

Very good and beautiful!

lei zhao

KAE

Useful

Mukalu Masaki

YOU WEIZHEN

Phuc Ho

### Phuc Ho (view profile)

Gustav Florentino

Procat

Chung Il Park

Excellent

David Franco

Fantastic!

Richelle Cabatic

### Richelle Cabatic (view profile)

Easy to use, thank you for this!

Shailesh Appukuttan

T Rockett

jian kang

### jian kang (view profile)

michele xiloyannis

Ray Lee

Gizem Okyay

DF

### DF (view profile)

Fantastic functions, works great in MATLAB R2014a and R2014b. Thank you!

DF

Gino Delfe

Matthew Hoehler

BreezyWest

Fannymost

### Fannymost (view profile)

I like it,very cool

minzhen li

Jing Wen

### Jing Wen (view profile)

Stephen Cobeldick

### Stephen Cobeldick (view profile)

Although the Author writes that "An Apache-Style Software License is included in the file" in fact there is absolutely no APACHE license mentioned anywhere in this download. The License supplied is in fact a BSD license, the only one supported by MATLAB File Exchange.

This submission breaks the license conditions of Cynthia Brewer's ColorBrewer colorschemes, which require her APACHE license to be included:

Tristan Ursell

cheng joylin

cheng joylin

Stefanos

Jamie

haiou

### haiou (view profile)

Stephen Cobeldick

### Stephen Cobeldick (view profile)

Note that this function breaks the original author's copyright as it does not include the copyright notice itself:

Yan Jiang

### Yan Jiang (view profile)

Very useful. It'll be even better if the colors close to yellow were darker.

VeraM

### VeraM (view profile)

Thank you for sharing this file, very useful!

Jeff Liu

Matthias Hunstig

### Matthias Hunstig (view profile)

This has been my standard colormap for lines for a while. However, I regularly end up changing the color of the yellow line - it is far to light to be well recognizable on a white background.

Guojin Feng

### Guojin Feng (view profile)

Thanks for this great function. This makes the figure a lot better and convenient.

Jonathan C. Lansey

### Jonathan C. Lansey (view profile)

Thanks everyone for the nice comments, I appreciate them all.
Also thanks Stephen for the suggestions.

denny

### denny (view profile)

Impressive. set it as my default

Stephen Cobeldick

### Stephen Cobeldick (view profile)

This might be of interest to the author, if they wish to improve the code of this submission:
http://www.mathworks.com/help/matlab/matlab_prog/vectorization.html

Stephen Cobeldick

### Stephen Cobeldick (view profile)

After the changes made by the author in response to my last comments, I though I would review this submission again. For the same points I made earlier:
1. The function now returns a MATLAB standard N*3 numeric colormap.
2. The default size is now 64, which still does not match the defaults of MATLAB's colormap functions.
3. There is now an H1 line (although perhaps the word 'colormap' should be included in this line?).
4. The mlint messages have been attended to (on MATLAB R2012b).
5. The examples are now complete!
6. Now calls "nargin", rather than the slow "exist".
7. "for" loops are still used to accomplish basic arithmetic. This is due to the data being stored in cell arrays. All of these "for" loops could be removed if the data was simply stored in numeric arrays.
8. Now preallocates the cell arrays before the "for" loops.
9. Unnecessary use of "varargin", when a simple second argument would suffice.
10. Improved examples.
12. ColorBrewer data is distributed without the required Apache license:

Most points have been addressed, and the submission basically does what it says. However the quality of the code is poor. In particular using cell arrays to store numeric data then requires multiple local functions to perform basic numeric arithmetic. MATLAB's name derives from 'MATrix LABoratory': it is designed to perform numeric operations on numeric matrices, fast! Storing the data in cell arrays and using multiple "for" loops to achieve basic numeric functionality is poor use of MATLAB, and as a result, this submission is larger and more complicated than it needs to be to provide this functionality.

Two stars for introducing MATLAB users to better colormaps than "jet" and "hsv".

Anne Urai

### Anne Urai (view profile)

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

G W

G W

G W

Andrew Tan

### Andrew Tan (view profile)

It's beautiful. Thank you!

Jeremy Manning

### Jeremy Manning (view profile)

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

lu li

### lu li (view profile)

Thanks a lot!

Jonathan C. Lansey

### Jonathan C. Lansey (view profile)

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.

Stephen Cobeldick

### Stephen Cobeldick (view profile)

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.

Stephen Cobeldick

### Stephen Cobeldick (view profile)

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;
colormap(cell2mat(linspecer(N)));
X = linspace(0,pi*3,1000);
Y = bsxfun(@(x,n)sin(x+2*n*pi/N), X.', 1:N);
plot(X,Y,'linewidth',5)