Main Content

更改曲面属性

以下示例演示如何在 MATLAB® 中获取曲面图的属性,以及如何更改属性值以自定义绘图。

曲面对象

在 MATLAB 中有多种方式可以创建曲面对象。一种方式是使用 surf

[X,Y,Z] = peaks(50);

figure
surf(X,Y,Z)

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

与所有图形对象一样,曲面有您可以查看和修改的属性。这些属性具有默认值。下面所示的曲面对象 s 显示了最常用的曲面属性,如 EdgeColorLineStyleFaceColorFaceLighting

s = surf(X,Y,Z)

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

s = 
  Surface with properties:

       EdgeColor: [0 0 0]
       LineStyle: '-'
       FaceColor: 'flat'
    FaceLighting: 'flat'
       FaceAlpha: 1
           XData: [50x50 double]
           YData: [50x50 double]
           ZData: [50x50 double]
           CData: [50x50 double]

  Use GET to show all properties

获取个别曲面属性

若要访问个别属性,请使用圆点表示法语法 object.PropertyName。例如,返回曲面的 FaceColor 属性。

s.FaceColor
ans = 
'flat'

更改常用的曲面属性

有些函数可用于更改曲面属性。例如,使用 shading 函数控制曲面着色。

shading interp    % interpolate the colormap across the surface face

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

使用 lighting 函数调整曲面的光照特性。要使 lighting 产生效果,您必须创建一个光源对象来照亮曲面。

light               % create a light
lighting gouraud    % preferred method for lighting curved surfaces

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

若要更改曲面的反射属性,请使用 material 函数。

material dull    % set material to be dull, no specular highlights

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

若要为当前坐标区中的所有对象设置透明度,请使用 alpha 函数。此函数将透明度设置为介于 1 和 0 之间的任意值,其中 1 表示完全不透明,0 表示完全透明。

alpha(0.8)    % set transparency to 0.8

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

更改其他曲面属性

若要自定义曲面的外观,请使用圆点表示法更改属性值。

CData 定义曲面顶点的颜色。FaceColor 属性指示如何从顶点颜色确定曲面颜色。

s.CData = hypot(X,Y);      % set color data

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

s.FaceColor = 'interp';    % interpolate to get face colors

AlphaData 定义曲面每个顶点的透明度。FaceAlpha 属性指示如何从顶点透明度确定曲面透明度。

s.AlphaData = gradient(Z);    % set vertex transparencies
s.FaceAlpha = 'interp';       % interpolate to get face transparencies

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

获取所有曲面属性

MATLAB 中的图形对象有许多属性。若要查看曲面的所有属性,请使用 get 命令。

get(s)
          AlignVertexCenters: off
                   AlphaData: [50x50 double]
            AlphaDataMapping: 'scaled'
             AmbientStrength: 0.3000
                  Annotation: [1x1 matlab.graphics.eventdata.Annotation]
            BackFaceLighting: 'reverselit'
                BeingDeleted: off
                  BusyAction: 'queue'
               ButtonDownFcn: ''
                       CData: [50x50 double]
                CDataMapping: 'scaled'
                   CDataMode: 'manual'
                 CDataSource: ''
                    Children: [0x0 GraphicsPlaceholder]
                    Clipping: on
                 ContextMenu: [0x0 GraphicsPlaceholder]
                   CreateFcn: ''
             DataTipTemplate: [1x1 matlab.graphics.datatip.DataTipTemplate]
                   DeleteFcn: ''
             DiffuseStrength: 0.8000
                 DisplayName: ''
                   EdgeAlpha: 1
                   EdgeColor: 'none'
                EdgeLighting: 'none'
                   FaceAlpha: 'interp'
                   FaceColor: 'interp'
                FaceLighting: 'gouraud'
                 FaceNormals: [49x49x3 double]
             FaceNormalsMode: 'auto'
            HandleVisibility: 'on'
                     HitTest: on
               Interruptible: on
                   LineStyle: '-'
                   LineWidth: 0.5000
                      Marker: 'none'
             MarkerEdgeColor: 'auto'
             MarkerFaceColor: 'none'
                  MarkerSize: 6
                   MeshStyle: 'both'
                      Parent: [1x1 Axes]
               PickableParts: 'visible'
                    Selected: off
          SelectionHighlight: on
    SpecularColorReflectance: 1
            SpecularExponent: 10
            SpecularStrength: 0
                         Tag: ''
                        Type: 'surface'
                    UserData: []
               VertexNormals: [50x50x3 double]
           VertexNormalsMode: 'auto'
                     Visible: on
                       XData: [50x50 double]
                   XDataMode: 'manual'
                 XDataSource: ''
                       YData: [50x50 double]
                   YDataMode: 'manual'
                 YDataSource: ''
                       ZData: [50x50 double]
                 ZDataSource: ''