colormap

View and set current colormap

Syntax

  • colormap(fig,'default')
  • colormap(ax,'default')
  • cmap = colormap example
  • cmap = colormap(fig)
  • cmap = colormap(ax) example

Description

example

colormap name sets the colormap for the current figure to the built-in colormap specified by name. The new colormap uses the same number of colors as the current colormap. The figure colormap affects all axes in the figure, unless you set an axes colormap separately.

example

colormap default uses the default colormap, which is the parula colormap with 64 colors. Versions of MATLAB® prior to R2014b use jet as the default.

example

colormap(map) sets the colormap for the current figure to the colormap specified by map. The figure colormap affects all axes in the figure, unless you set an axes colormap separately. Use this syntax if you want to use a built-in colormap with a specific number of colors or if you want to use a custom colormap.

  • For a built-in colormap with a specific number of colors, specify map as one of the built-in colormap functions and it pass an integer value as an input argument. For example, colormap(summer(10)) uses 10 colors from the summer colormap. If you do not specify the number of colors, such as colormap(summer), then the colormap contains the same number of colors as the current colormap.

  • For a custom colormap, specify map as a three-column matrix of values in the range [0,1] where each row is an RGB triplet that defines one color of the colormap. An RGB triplet is a three-element row vector specifying the red, green, and blue intensities for a color.

colormap(fig,map) sets the colormap for the figure specified by fig.

example

colormap(ax,map) sets the colormap for the axes specified by ax. Each axes within a figure can have a unique colormap. After you set an axes colormap, changing the figure colormap does not affect the axes.

colormap(fig,'default') sets the colormap for the figure specified by fig to the default colormap.

colormap(ax,'default') sets the colormap for the axes specified by ax to the default colormap.

example

cmap = colormap returns the three-column matrix of RGB triplets defining the colormap for the current figure.

cmap = colormap(fig) returns the colormap for the figure specified by fig.

example

cmap = colormap(ax) returns the colormap for the axes specified by ax.

Examples

expand all

Change Colormap for Figure

Create a surface plot and set the colormap to winter.

figure
surf(peaks)
colormap winter

Set Colormap Back to Default

First, change the colormap for the current figure to summer.

figure
surf(peaks)
colormap summer

Now set the colormap back to your system's default value. If you have not specified a different default value, then the default colormap is parula.

colormap default

Use Different Colormaps for Each Axes in Figure

Create a figure with two subplots and store the axes handles, ax1 and ax2. Use a different colormap for each axes by passing the axes handles to the colormap function. In the upper subplot, create a surface plot using the spring colormap. In the lower subplot, create a surface plot using the winter colormap.

figure
ax1 = subplot(2,1,1);
surf(peaks)
colormap(ax1,spring)

ax2 = subplot(2,1,2);
surf(peaks)
colormap(ax2,winter)

Specify Number of Colors for Colormap

Specify the number of colors used in a colormap by passing an integer as an input argument to the built-in colormap. Use five colors from the parula colormap.

figure
mesh(peaks)
colormap(parula(5))

Create Custom Colormap

Create a custom colormap by defining a three-column matrix of values between 0.0 and 1.0. Each row defines a three-element RGB triplet. The first column specifies the red intensities. The second column specifies the green intensities. The third column specifies the blue intensities.

Use a colormap of blue values by setting the first two columns to zeros.

map = [0, 0, 0.3
    0, 0, 0.4
    0, 0, 0.5
    0, 0, 0.6
    0, 0, 0.8
    0, 0, 1.0];

figure
surf(peaks)
colormap(map)

Return Colormap Values Used in Plot

Create a surface plot of the peaks function and specify a colormap.

figure
mesh(peaks)
colormap(autumn(5))

Return the three-column matrix of values that define the colors used in the plot. Each row is an RGB triplet color value that specifies one color of the colormap.

cmap = colormap
cmap =

    1.0000         0         0
    1.0000    0.2500         0
    1.0000    0.5000         0
    1.0000    0.7500         0
    1.0000    1.0000         0

Return Colormap Values for Specific Axes

Return the colormap values for a specific axes by passing its axes handle to the colormap function.

Create a figure with two subplots and return the axes handles, ax1 and ax2. Add a filled contour plot to each axes and use a different colormap for each axes.

figure
ax1 = subplot(2,1,1);
contourf(peaks)
colormap(ax1,hot(8));

ax2 = subplot(2,1,2);
contourf(peaks)
colormap(ax2,pink);

Return the colormap values used in the upper subplot by passing its axes handle, ax1, to the colormap function. Each row is an RGB triplet color value that specifies one color of the colormap.

cmap = colormap(ax1)
cmap =

    0.3333         0         0
    0.6667         0         0
    1.0000         0         0
    1.0000    0.3333         0
    1.0000    0.6667         0
    1.0000    1.0000         0
    1.0000    1.0000    0.5000
    1.0000    1.0000    1.0000

Change Colormap for Figure with Image

Load the spine data set that returns the image X and its associated colormap map. Display X using the image function and set the colormap to map.

load spine
figure
image(X)
colormap(map)

Input Arguments

expand all

name — Built-in colormapparula (default) | built-in colormap

Built-in colormap, specified as a one of the colormaps listed in the table. The default colormap is parula. Versions of MATLAB prior to R2014b use jet as the default.

The colormap function applies the new colormap with the same number of colors as the current colormap. To change the number of colors, use the map input argument.

This table lists the built-in colormaps.

Colormap NameColor Scale
parula

jet

hsv

hot

cool

spring

summer

autumn

winter

gray

bone

copper

pink

lines

colorcube

prism

flag

white

map — Built-in or custom colormapparula (default) | built-in colormap function | three-column matrix of RGB triplets

Built-in or custom colormap, specified as one of the built-in colormap functions or a three-column matrix of RGB triplets. The default colormap is parula. Versions of MATLAB prior to R2014b use jet as the default.

Built-In Colormap Functions

To use a built-in colormap with a specific number of colors, specify map as one of the built-in colormaps functions and pass it an integer value as an input argument. For example, colormap(parula(12)) uses a map with 12 colors from the parula colormap. If you do not specify the number of colors, such as colormap(parula), then MATLAB uses the same number of colors as the current colormap.

    Note:   Both the name and the map input arguments provide the same built-in colormap options. However, if you use the input argument name, then the colormap function processes name as a string and you cannot control the number of colors. If you use the input argument map, then the colormap function processes map as a function, which means that you can pass the built-in colormap an integer value to control the number of colors.

This table lists the built-in colormaps functions.

Colormap NameColor Scale
parula

jet

hsv

hot

cool

spring

summer

autumn

winter

gray

bone

copper

pink

lines

colorcube

prism

flag

white

Custom Colormap

To create a custom colormap, specify map as a three-column matrix of RGB triplets where each row defines one color. An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]. For example, this matrix defines a colormap containing five colors.

map = [0.2, 0.1, 0.5
    0.1, 0.5, 0.8
    0.2, 0.7, 0.6
    0.8, 0.7, 0.3
    0.9, 1, 0];

This table lists the RGB triplet values for common colors.

ColorRGB Triplet
yellow[1,1,0]
magenta[1,0,1]
cyan[0,1,1]
red[1,0,0]
green[0,1,0]
blue[0,0,1]
white[1,1,1]
black[0,0,0]

fig — Figure objectfigure object

Figure object. The figure colormap affects plots for all axes within the figure, unless you specify an axes colormap separately.

Each figure has a Colormap property. When you change the figure colormap using the colormap function, MATLAB updates the Colormap property of the figure.

ax — Axes objectaxes object

Axes object. You can define a unique colormap for each axes. After you set an axes colormap, changing the figure colormap does not affect the axes.

Output Arguments

expand all

cmap — Colormap valuesthree-column matrix of RGB triplets

Colormap values, returned as a three-column matrix of RGB triplets. Each row of the matrix defines one RGB triplet that specifies one color of the colormap. The values are in the range [0,1].

More About

expand all

Tips

  • To control the mapping of data values into the colormap, use the caxis function.

Was this topic helpful?