# Making Color Ramps in Matlab

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

When visualizing an array of data in a heatmap, a good color map makes a world of difference.**Odd Hypothesis**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Thanks to my work in ‘omics (i.e. transcriptomics – microarrays and RNASeq) I’ve looked at a lot of heatmaps over the past couple of years, and generated quite a few to boot. Back in my Matlab heavy grad school days, I was generally happy with the default ‘jet’ color scheme (which given it’s double rainbow-eseque aesthetics would make some individuals on this planet overly emotional). Suffice it to say, I was a bit wary of straying far from the available maps (the others I used semi-regularly were “bone”, “gray”, and “hot”).

Today I needed to create a nice color ramp in a GUI tool I’ve developed in Matlab for a dataset that spanned [-Inf, Inf]. Ideally, it should have three color stops:

- a “cool” color for extreme negative values
- a neutral color for 0
- a “hot” color for extreme positive values

- blue
- black
- yellow

If I were generating this ramp in R it would be quite trivial with the colorRampPalette() function:

bky.ramp = colorRampPalette(c('blue', 'black', 'yellow'))

The above line would create a function bky.ramp() that you could use to specify a ramping palette of arbitrary length for a heatmap() (or any other plotting function):

heatmap(X, col=bky.ramp(256))

Doing this in Matlab is similar, but a tad more obscure. If you look at the help for the colormap() function it says:

A colormap is anI know that the colors I need are:m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color. Thekth row of the colormap defines thekth color, wheremap(k,:)=[r(k)g(k)b(k)])specifies the intensity of red, green, and blue.colormap(map)sets the colormap to the matrixmap. If any values inmapare outside the interval [0 1], you receive the errorColormap must have values in [0,1].

- blue = [0 0 1]
- black = [0 0 0]
- yellow = [1 1 0]

interp1 1-D interpolation (table lookup)In its simplest invocation, it does linear interpolation between supplied points in

YI = interp1(X,Y,XI) interpolates to find YI, the values of the

underlying function Y at the points in the array XI. X must be a

vector of length N.

If Y is a vector, then it must also have length N, and YI is the

same size as XI. If Y is an array of size [N,D1,D2,…,Dk], then

the interpolation is performed for each D1-by-D2-by-…-Dk value

in Y(i,:,:,…,:).

If XI is a vector of length M, then YI has size [M,D1,D2,…,Dk].

If XI is an array of size [M1,M2,…,Mj], then YI is of size

[M1,M2,…,Mj,D1,D2,…,Dk].

`Y`over points

`XI`. How is this used to create a BKY color ramp with 256 levels? Like so:

bkyramp = interp1([blue; black; yellow], linspace(1,3,256));

If you’re the type that likes to encapsulate things in reusable functions (which I am), you end up with something like this:

To

**leave a comment**for the author, please follow the link and comment on their blog:**Odd Hypothesis**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.