Main Content

使用颜色图更改颜色方案

MATLAB® 在显示曲面绘图等视觉呈现时使用默认的颜色方案。您可以通过指定颜色图来更改颜色方案。颜色图是包含 RGB 三元组的三列数组,其中每一行定义一种不同的颜色。

例如,此处为使用默认颜色方案的曲面绘图。

f = figure;
surf(peaks);

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

以下命令可将当前图窗的颜色图更改为 winter,它是多种预定义颜色图中的一种(有关预定义颜色图的完整列表,请参阅颜色图)。

colormap winter;

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

如果您打开了多个图窗,可将 Figure 对象作为第一个参量传递给 colormap 函数。

colormap(f,hot);

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

默认情况下,每个预定义的颜色图都会提供一个包含 256 种颜色的调色板。但是,您可以向预定义的颜色图函数传递一个整数来指定所需数量的颜色。例如,此处为包含十个条目的 hot 颜色图。

c = hot(10);
colormap(c);

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

您也可以自行创建 M×3 数组形式的颜色图。此数组中的各行包含不同颜色强度的红色、绿色和蓝色。颜色强度介于范围 [0,1] 之间。此处为一个简单的颜色图,其中包含三个条目。

mycolors = [1 0 0; 1 1 0; 0 0 1];
colormap(mycolors);

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

如果使用多个坐标区,可以通过将坐标区对象传递给 colormap 函数,为每个坐标区分配不同的颜色图。

tiledlayout(1,2)
ax1 = nexttile;
surf(peaks);
shading interp;
colormap(ax1,parula(10));

ax2 = nexttile;
surf(peaks);
shading interp;
colormap(ax2,cool(10));

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

相关主题