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 = colormapreturns the current colormap of the current figurecolormap(map)sets the map for the current figurecolormap(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
| Name | Description |
|---|---|
parula | Default since R2014b; perceptually uniform, blue→yellow |
turbo | Improved rainbow; preferred over jet |
jet | Classic rainbow (legacy); avoid for sequential data |
hsv | Full hue cycle |
hot | Black→red→yellow→white |
cool | Cyan→magenta |
spring, summer, autumn, winter | Two-tone gradients |
gray | Grayscale |
bone | Grayscale with a slight blue tint |
copper | Black→copper |
pink | Pastel pink to white |
lines | Discrete 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 range1..size(cmap,1)and looked up in the active colormap clim([cmin cmax])(formerlycaxis) 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)
colorbarCustom 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)