Main Content

修改图形对象

以下示例演示如何在 MATLAB® 中创建、显示和修改图形对象。

图形对象

当 MATLAB 创建一个绘图时,它会创建一系列的图形对象。图形对象的例子有图窗、坐标区、线条、填充和文本。下图有三个图形对象 -- 一个坐标区、一条线条和一个文本对象。使用可选输出参量存储所创建的图形对象。

x = -pi:pi/20:pi;
y = sin(x);

f = figure;
p = plot(x,y);
txt1 = text(0.2,0,'sin(x)');

Figure contains an axes object. The axes object contains 2 objects of type line, text.

所有图形对象均有您可以查看和修改的属性。这些属性具有默认值。下面所示的线条对象 p 显示了最常用的线条属性,如 ColorLineStyleLineWidth

p
p = 
  Line with properties:

              Color: [0 0.4470 0.7410]
          LineStyle: '-'
          LineWidth: 0.5000
             Marker: 'none'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: [-3.1416 -2.9845 -2.8274 -2.6704 -2.5133 -2.3562 -2.1991 -2.0420 -1.8850 -1.7279 -1.5708 -1.4137 -1.2566 -1.0996 -0.9425 -0.7854 -0.6283 -0.4712 -0.3142 -0.1571 0 0.1571 0.3142 0.4712 0.6283 0.7854 0.9425 1.0996 ... ] (1x41 double)
              YData: [-1.2246e-16 -0.1564 -0.3090 -0.4540 -0.5878 -0.7071 -0.8090 -0.8910 -0.9511 -0.9877 -1 -0.9877 -0.9511 -0.8910 -0.8090 -0.7071 -0.5878 -0.4540 -0.3090 -0.1564 0 0.1564 0.3090 0.4540 0.5878 0.7071 0.8090 0.8910 ... ] (1x41 double)

  Use GET to show all properties

如果用于创建对象的命令缺少了分号,MATLAB 会以相同的方式显示内容。

txt2 = text(x(end), y(end), 'pi')

Figure contains an axes object. The axes object contains 3 objects of type line, text.

txt2 = 
  Text (pi) with properties:

                 String: 'pi'
               FontSize: 10
             FontWeight: 'normal'
               FontName: 'Helvetica'
                  Color: [0 0 0]
    HorizontalAlignment: 'left'
               Position: [3.1416 1.2246e-16 0]
                  Units: 'data'

  Use GET to show all properties

获取图形对象属性

若要访问图形对象的个别属性,请使用圆点表示法语法 object.PropertyName。例如,返回线条对象的 LineWidth 属性。

pcol = p.LineWidth
pcol = 0.5000

通过设置线条的 Color 属性将其颜色更改为红色。

p.Color = 'red';

Figure contains an axes object. The axes object contains 3 objects of type line, text.

父级和子级

MATLAB 按一定的层次结构排列图形对象。层次结构的顶部是称为图形根的特殊对象。若要访问图形根,请使用 groot 函数。

groot
ans = 
  Graphics Root with properties:

          CurrentFigure: [1x1 Figure]
    ScreenPixelsPerInch: 100
             ScreenSize: [1 1 1280 1024]
       MonitorPositions: [1 1 1280 1024]
                  Units: 'pixels'

  Use GET to show all properties

所有图形对象(除了根)均有一个父级。例如,坐标区的父级是一个图窗。

ax = gca;
ax.Parent
ans = 
  Figure (1) with properties:

      Number: 1
        Name: ''
       Color: [1 1 1]
    Position: [348 376 583 437]
       Units: 'pixels'

  Use GET to show all properties

许多对象也有子级。此套坐标区有三个子级 - 两个文本对象和一个线条对象。

ax.Children
ans = 
  3x1 graphics array:

  Text    (pi)
  Text    (sin(x))
  Line

因为坐标区有多个子级,Children 属性的值是一个图形对象数组。要访问坐标区的个别子级,请对数组建立索引。然后,您可以设置子级对象的属性。

t = ax.Children(2);
t.FontWeight = 'bold';

Figure contains an axes object. The axes object contains 3 objects of type line, text.

预分配图形对象数组

在 MATLAB 中有一个最佳做法,是在使用数组前先进行预分配。使用 gobjects 命令预分配图形对象数组。然后,您可以将图形对象添加到该数组中。

objarray = gobjects(1,5);
objarray(1) = f;
objarray(2) = ax;
objarray(3) = p;
objarray(4) = txt1;
objarray(5) = txt2;
objarray
objarray = 
  1x5 graphics array:

    Figure    Axes      Line      Text      Text  

获取所有对象属性

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

get(f)
                 Alphamap: [0 0.0159 0.0317 0.0476 0.0635 0.0794 0.0952 0.1111 0.1270 0.1429 0.1587 0.1746 0.1905 0.2063 0.2222 0.2381 0.2540 0.2698 0.2857 0.3016 0.3175 0.3333 0.3492 0.3651 0.3810 0.3968 0.4127 0.4286 0.4444 0.4603 ... ] (1x64 double)
             BeingDeleted: off
               BusyAction: 'queue'
            ButtonDownFcn: ''
                 Children: [1x1 Axes]
                 Clipping: on
          CloseRequestFcn: 'closereq'
                    Color: [1 1 1]
                 Colormap: [256x3 double]
              ContextMenu: [0x0 GraphicsPlaceholder]
                CreateFcn: ''
              CurrentAxes: [1x1 Axes]
         CurrentCharacter: ''
            CurrentObject: [0x0 GraphicsPlaceholder]
             CurrentPoint: [0 0]
                DeleteFcn: ''
             DockControls: on
                 FileName: ''
        GraphicsSmoothing: on
         HandleVisibility: 'on'
                     Icon: ''
            InnerPosition: [348 376 583 437]
            IntegerHandle: on
            Interruptible: on
           InvertHardcopy: on
              KeyPressFcn: ''
            KeyReleaseFcn: ''
                  MenuBar: 'none'
                     Name: ''
                 NextPlot: 'add'
                   Number: 1
              NumberTitle: on
            OuterPosition: [348 376 583 437]
         PaperOrientation: 'portrait'
            PaperPosition: [1.3350 3.3150 5.8300 4.3700]
        PaperPositionMode: 'auto'
                PaperSize: [8.5000 11]
                PaperType: 'usletter'
               PaperUnits: 'inches'
                   Parent: [1x1 Root]
                  Pointer: 'arrow'
        PointerShapeCData: [16x16 double]
      PointerShapeHotSpot: [1 1]
                 Position: [348 376 583 437]
                 Renderer: 'opengl'
             RendererMode: 'auto'
                   Resize: on
               Scrollable: off
            SelectionType: 'normal'
           SizeChangedFcn: ''
                      Tag: ''
                  ToolBar: 'none'
                     Type: 'figure'
                    Units: 'pixels'
                 UserData: []
                  Visible: off
      WindowButtonDownFcn: ''
    WindowButtonMotionFcn: ''
        WindowButtonUpFcn: ''
        WindowKeyPressFcn: ''
      WindowKeyReleaseFcn: ''
     WindowScrollWheelFcn: ''
              WindowState: 'normal'
              WindowStyle: 'normal'
                 XDisplay: ':99'