Main Content

曲面绘图数据与颜色图的关系

在使用 surfmesh 等函数创建曲面绘图时,可以通过调用 colormap 函数自定义颜色方案。如果要进一步控制外观,可以更改穿过此曲面的颜色的方向或模式。这种自定义需要更改控制曲面和颜色图之间关系的数组中的值。

曲面与颜色图之间的关系

Surface 对象的 CData 属性包含一个索引数组 C,此数组将绘图中的特定位置与颜色图中的颜色相关联。C 与曲面 z = f(x,y) 具有如下关系:

  • C 的大小与 Z 相同,其中 Z 是在曲面上每个网格点处都包含 f(x,y) 值的数组。

  • C(i,j) 处的值控制曲面上网格位置 (i,j) 的颜色。

  • 默认情况下,C 等于 Z,即颜色随海拔而异。

  • 默认情况下,C 的范围线性映射到颜色图数组中的行数。

举例来说,Z = X + Y 的一个 3×3 抽样与包含 N 个条目的颜色图具有下列关系。

The relationship between matrix C, a colormap matrix, and a surface plot. The smallest value of C maps to the first row in the colormap matrix, and that color appears at the corresponding location of the plot. Similarly, the largest value of C maps to the last row in the colormap matrix, and that color appears at the corresponding location of the plot.

请注意,最小值 (-2) 映射到颜色图中的第一行。最大值 (2) 映射到颜色图中的最后一行。C 中的中间值线性映射到颜色图中间的行。

注意

先前的曲面绘图说明颜色如何分配给曲面上的顶点,但默认行为是用纯色填充补片面。该种纯色基于分配给周围顶点的颜色。有关详细信息,请参阅 FaceColor 的属性说明。

更改颜色的方向或模式

在使用 C=Z 的默认值时,颜色随 Z 的变化而异。

[X,Y] = meshgrid(-10:10);
Z = X + Y;
s = surf(X,Y,Z);
xlabel('X');
ylabel('Y');
zlabel('Z');

Figure contains an axes object. The axes object with xlabel X, ylabel Y contains an object of type surface.

在创建曲面时通过指定 C 可以改变这种行为。例如,以下曲面上的颜色随 X 而异。

C = X;
s = surf(X,Y,Z,C);
xlabel('X');
ylabel('Y');
zlabel('Z');

Figure contains an axes object. The axes object with xlabel X, ylabel Y contains an object of type surface.

您也可以直接设置 CData 属性。以下命令将使得颜色随 Y 而异。

s.CData = Y;

Figure contains an axes object. The axes object with xlabel X, ylabel Y contains an object of type surface.

颜色无需遵循单一维度的变化。实际上,CData 可以是任何Z 大小相同的数组。例如,以下平面上的颜色遵循 sinc 函数形状。

R = sqrt(X.^2 + Y.^2) + eps;
s.CData = sin(R)./(R);

Figure contains an axes object. The axes object with xlabel X, ylabel Y contains an object of type surface.

另请参阅

函数

属性

相关主题