This example shows how to use colormaps in MATLAB®.
|On this page…|
A colormap is a three-column matrix of real numbers. Each row in the matrix is an RGB triplet value that defines one color in the colormap. MATLAB draws surfaces and other objects by mapping the data that defines the surface into the colors in the colormap.
The picture below shows a surface drawn with the default colormap, which is called parula.
Parula is the default colormap in MATLAB because it has a natural perceptual ordering (dark to light) and because it is perceptually uniform. Because of this property, smooth changes in the data are seen as smooth changes in color, while sharp changes in the data are seen as sharp changes in color.
Each built-in colormap has a corresponding function that returns the matrix of RGB triplets. The default size of a colormap is 64 colors.
my_colormap = parula; size(my_colormap)
ans = 64 3
To change the colormap, use the colormap function. The colormap function takes a single input argument which is either the name of a built-in colormap or a three-column matrix of RGB triplet values. By default, the colormap function changes the colormap for everything in the current figure.
To change the colormap for a specific figure or a specific axes within a figure, call the colormap function with a figure or axes object as the first argument. For example, use a different colormap for each axes in the figure.
ax1 = subplot(2,1,1); surf(peaks(15)) colormap(ax1, autumn) ax2 = subplot(2,1,2); surf(peaks(15)) colormap(ax2, winter)
The size of the colormap determines how smooth the transition is between colors. A colormap with a small number of colors has sharp transitions between colors. A larger colormap provides much smoother transitions between colors. To change the number of colors in the colormap, pass an integer value to the built-in colormap.
clf surf(peaks) shading interp colormap(jet(10))
To use a larger colormap, pass a larger value to the built-in colormap.