MATLAB Graphics Colormap

A colormap is an m-by-3 matrix of [R G B] rows in [0,1]. It is the bridge between scalar data (the c argument of plotting functions, the height-value of surf, etc.) and the rendered color: scalar values are rescaled to row indices and looked up in the active colormap.

Setting and Reading the Active Map

  • cmap = colormap returns the current colormap of the current figure
  • colormap(map) sets the map for the current figure
  • colormap(ax,map) sets the map for a specific axes
  • map may be a built-in name (char/string), a built-in handle (parula, gray, …), or any m-by-3 matrix

Each predefined name is also a function: parula(64) returns the 64-row matrix that colormap parula would install.

Predefined Maps

NameDescription
parulaDefault since R2014b; perceptually uniform, blue→yellow
turboImproved rainbow; preferred over jet
jetClassic rainbow (legacy); avoid for sequential data
hsvFull hue cycle
hotBlack→red→yellow→white
coolCyan→magenta
spring, summer, autumn, winterTwo-tone gradients
grayGrayscale
boneGrayscale with a slight blue tint
copperBlack→copper
pinkPastel pink to white
linesDiscrete categorical palette (uses axes ColorOrder)

How Color Indexing Works

When a plotting function accepts scalar/vector color data (e.g. scatter(x,y,sz,c), imagesc, surf):

  • Values are linearly rescaled from the axes CLim (color limits) onto the row index range 1..size(cmap,1) and looked up in the active colormap
  • clim([cmin cmax]) (formerly caxis) overrides the rescaling range
  • A three-column c of RGB triplets bypasses the colormap entirely
x = 1:100;
y = sin(x/5);
c = x;                 % 1..100, becomes a row index into the colormap
scatter(x, y, 30, c, 'filled')
colormap(turbo)
colorbar

Custom Maps

A map is just an m-by-3 matrix. A 64-step black-to-red ramp:

n = 64;
cmap = [linspace(0,1,n)' zeros(n,2)];
colormap(cmap)