Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

colormap

查看并设置当前颜色图

说明

设置颜色图

示例

colormap map 将当前图窗的颜色图设置为指定的预定义颜色图。例如,colormap hot 将颜色图设置为 hot

如果您为图窗设置了颜色图,图窗中的坐标区和图将使用相同的颜色图。新颜色图的长度(颜色数)与当前颜色图相同。当您使用此语法时,不能为颜色图指定自定义长度。有关颜色图的详细信息,请参阅详细信息部分。

示例

colormap(map) 将当前图窗的颜色图设置为 map 指定的颜色图。

示例

colormap(target,map)target 指定的图窗、坐标区或独立可视化设置颜色图,而不是为当前图窗设置颜色图。

cmap = colormap(___) 设置颜色图,并将其以由 RGB 三元组组成的三列矩阵形式返回。使用上述任一使用圆括号的语法将 cmap 指定为输出参数。

获取当前颜色图

示例

cmap = colormap 返回当前图窗的颜色图,形式为 RGB 三元组组成的三列矩阵。

示例

cmap = colormap(target) 返回由 target 指定的图窗、坐标区或独立可视化的颜色图。

示例

全部折叠

创建一个曲面图并将颜色图设置为 winter

surf(peaks)
colormap winter

Figure contains an axes object. The axes object contains an object of type surface.

首先,将当前图窗的颜色图更改为 summer

surf(peaks)
colormap summer

Figure contains an axes object. The axes object contains an object of type surface.

现在将颜色图设置回您系统的默认值。如果您尚未指定不同默认值,则默认颜色图是 parula

colormap default

Figure contains an axes object. The axes object contains an object of type surface.

从 R2019b 开始,您可以使用 tiledlayoutnexttile 函数显示分块图。调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1ax2。通过将坐标区对象传递给 colormap 函数,为每个坐标区指定不同的颜色图。在上坐标区中,使用 spring 颜色图创建一个曲面图。在下坐标区中,使用 winter 颜色图创建一个曲面图。

tiledlayout(2,1)
ax1 = nexttile;
surf(peaks)
colormap(ax1,spring)

ax2 = nexttile; 
surf(peaks)
colormap(ax2,winter)

Figure contains 2 axes objects. Axes object 1 contains an object of type surface. Axes object 2 contains an object of type surface.

通过将整数作为内置颜色图的输入参数传递来指定用于颜色图的颜色数。使用 parula 颜色图中的五种颜色。

mesh(peaks)
colormap(parula(5))

Figure contains an axes object. The axes object contains an object of type surface.

通过定义一个由介于 0.0 和 1.0 之间的值组成的三列矩阵来创建一个自定义颜色图。每行定义一个三元素 RGB 三元组。第一列指定红色强度。第二列指定绿色强度。第三列指定蓝色强度。

通过将前两个列设置为零来使用蓝色值的颜色图。

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];

surf(peaks)
colormap(map)

Figure contains an axes object. The axes object contains an object of type surface.

创建 peaks 函数的曲面图并指定颜色图。

mesh(peaks)
colormap(autumn(5))

Figure contains an axes object. The axes object contains an object of type surface.

返回定义用在绘图中使用的颜色的值的三列矩阵。每行是一个指定颜色图的一种颜色的 RGB 三元组颜色值。

cmap = colormap
cmap = 5×3

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

通过将坐标区对象传递给 colormap 函数,返回特定坐标区的颜色图值。

使用 tiledlayoutnexttile 函数创建两个分块图,这两个函数是从 R2019b 开始推出的新函数。调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1ax2。然后显示两个以不同颜色图填充的等高线图。

tiledlayout(2,1)
ax1 = nexttile;
contourf(peaks)
colormap(ax1,hot(8))

ax2 = nexttile;
contourf(peaks)
colormap(ax2,pink)

Figure contains 2 axes objects. Axes object 1 contains an object of type contour. Axes object 2 contains an object of type contour.

通过将 ax1 传递给 colormap 函数,返回上部绘图中使用的颜色图值。每行是一个指定颜色图的一种颜色的 RGB 三元组颜色值。

cmap = colormap(ax1)
cmap = 8×3

    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

加载 spine 数据集以返回 X 及其关联的颜色图 map。使用 image 函数显示 X 并将颜色图设置为 map

load spine
image(X)
colormap(map)

Figure contains an axes object. The axes object contains an object of type image.

输入参数

全部折叠

新颜色方案的颜色图,指定为颜色图名称、由 RGB 三元组组成的三列矩阵或 'default'。颜色图名称指定一个与当前颜色图具有相同颜色数的预定义颜色图。由 RGB 三元组组成的三列矩阵指定一个自定义颜色图。您可以自行创建该矩阵,也可以调用一个预定义的颜色图函数来创建矩阵。例如,colormap(parula(10)) 将当前图窗的颜色图设置为从 parula 颜色图中选择的 10 种颜色。

'default' 将目标对象的颜色图设置为默认颜色图。

颜色图名称

下表列出了预定义的颜色图。

颜色图名称色阶

parula

Colorbar showing the colors of the parula colormap. The colormap starts at dark blue and transitions to lighter blue, green, orange and yellow. The transitions between colors are more perceptually uniform than in most other colormaps.

turbo

Colorbar showing the colors of the turbo colormap. The colormap starts at dark blue and transitions to lighter blue, bright green, orange, yellow, and dark red. This colormap is similar to jet, but the transitions between colors are more perceptually uniform than in jet.

hsv

Colorbar showing the colors of the hsv colormap. The colormap starts at red and transitions to yellow, bright green, cyan, dark blue, magenta, and bright orange.

hot

Colorbar showing the colors of the hot colormap. The colormap starts at dark red and transitions to bright red, orange, yellow, and white.

cool

Colorbar showing the colors of the cool colormap. The colormap starts at cyan and transitions to light blue, light purple, and magenta.

spring

Colorbar showing the colors of the spring colormap. The colormap starts at magenta and transitions to pink, light orange, and yellow.

summer

Colorbar showing the colors of the summer colormap. The colormap starts at medium green and transitions to yellow.

autumn

Colorbar showing the colors of the autumn colormap. The colormap starts at bright orange and transitions to yellow.

winter

Colorbar showing the colors of the winter colormap. The colormap starts at dark blue and transitions to bright green.

gray

Colorbar showing the gray colormap. The colormap starts at black and transitions to white.

bone

Colorbar showing the bone colormap. This colormap has colors that are approximately gray with a slight blue color tint. The colormap starts at dark gray and transitions to white.

copper

Colorbar showing the copper colormap. This colormap starts at black and transitions to a medium orange, similar to the color of copper.

pink

Colorbar showing the pink colormap. This colormap starts at dark red and transitions to dark pink, tan, and white.

sky (自 R2023a 起)

Colorbar showing the sky colormap. This colormap starts at a very light shade of blue and transitions to a darker shade of blue.

abyss (自 R2023b 起)

Colorbar showing the abyss colormap. This colormap starts at a very dark shade of blue and transitions to a lighter shade of blue.

jet

Colorbar showing the colors of the jet colormap. The colormap starts at dark blue and transitions to light blue, bright green, orange, yellow, and dark red.

lines

Colorbar showing the colors of the lines colormap. The colormap contains a repeating pattern of colors: dark blue, dark orange, dark yellow, dark purple, medium green, light blue, and dark red.

colorcube

Colorbar showing the colors of the colorcube colormap. The colormap is a course sampling of the RGB colorspace.

prism

Colorbar showing the colors of the prism colormap. The colormap contains a repeating pattern of colors: red, orange, yellow, green, blue, and purple.

flag

Colorbar showing the colors of the flag colormap. The colormap contains a repeating pattern of colors: red, white, blue, and black.

white

Colorbar showing the white colormap, which is entirely white.

三列矩阵

要创建自定义颜色图,请将 map 指定为 RGB 三元组组成的三列矩阵(其中每行定义一种颜色)。RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度可以是 [0, 1] 范围内的 doublesingle 值,也可以是 [0, 255] 范围内的 uint8 值。例如,此矩阵定义一个包含五种颜色的颜色图。

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];

下表列出了常见颜色的 RGB 三元组值。

颜色doublesingle RGB 三元组uint8 RGB 三元组
黄色[1 1 0][255 255 0]
品红色[1 0 1][255 0 255]
青蓝色[0 1 1][0 255 255]
红色[1 0 0][255 0 0]
绿色[0 1 0][0 255 0]
蓝色[0 0 1][0 0 255]
白色[1 1 1][255 255 255]
黑色[0 0 0][0 0 0]

数据类型: char | double | single | uint8

目标,指定为下列值之一:

  • Figure 对象。图窗的颜色图作用于图窗中所有坐标区上的绘图。

  • Axes 对象、PolarAxes 对象或 GeographicAxes 对象。您可以为图窗中的每个坐标区定义一个唯一的颜色图。

  • 具有 Colormap 属性的独立可视化。例如,您可以更改或查询 HeatmapChart 对象的颜色图。

输出参数

全部折叠

颜色图值,以由 RGB 三元组组成的三列矩阵形式返回。矩阵的每行定义一个指定颜色图的一种颜色的 RGB 三元组。这些值在 [0, 1] 范围内。

详细信息

全部折叠

颜色图

颜色图是由值组成的矩阵,这些值用于定义诸如曲面、图像以及补片之类的图形对象的颜色。MATLAB® 通过将数据值映射到颜色图中的颜色来绘制这些对象。

颜色图可以为任意长度,但宽度必须为三列。矩阵中的每一行均使用 RGB 三元组定义一种颜色。RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。通常,强度是 [0, 1] 范围内的 doublesingle 值。值 0 表示无颜色,值 1 表示全强度。例如,以下命令将创建一个包含五种颜色(黑色、红色、绿色、蓝色和白色)的颜色图。

mymap = [0 0 0
    1 0 0
    0 1 0
    0 0 1
    1 1 1];

要更改可视化的颜色方案,请调用 colormap 函数以更改所属坐标区或图窗的颜色图。例如,以下命令将创建一个曲面图,并将图窗的颜色图设置为 mymap

surf(peaks)
colormap(mymap)

Surface plotted with a custom colormap containing five colors: black, red, green, blue, and black.

提示

  • 要控制颜色图的范围及其与数据范围的关系,请使用 clim 函数。

    在 R2022a 之前: 使用 caxis,它具有与 clim 相同的语法和参数。

版本历史记录

在 R2006a 之前推出

全部展开