Main Content

그래픽스 객체 수정하기

이 예제에서는 MATLAB®에서 그래픽스 객체를 생성하고, 표시하고, 수정하는 방법을 보여줍니다.

그래픽스 객체

MATLAB은 플롯을 만들 때 일련의 그래픽스 객체를 만듭니다. Figure, axes, line, patch, text는 그래픽스 객체의 예입니다. 아래 그림에는 세 개의 그래픽스 객체 즉, axes, line, text 객체가 있습니다. 생성된 그래픽스 객체를 저장하려면 선택적 출력 인수를 사용하십시오.

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.

모든 그래픽스 객체는 속성을 가지며, 사용자는 이러한 속성을 확인하거나 수정할 수 있습니다. 이러한 속성은 디폴트 값을 가집니다. line 객체 p를 표시하면 Color, LineStyle, LineWidth 등과 같이 가장 일반적으로 사용되는 line 속성을 볼 수 있습니다.

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을 사용하십시오. 예를 들어, 다음 명령은 line 객체의 LineWidth 속성을 반환합니다.

pcol = p.LineWidth
pcol = 0.5000

line 객체의 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

루트를 제외한 모든 그래픽스 객체는 부모를 가집니다. 예를 들어, 좌표축의 부모는 Figure입니다.

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

많은 객체들은 또한 자식도 가집니다. 이 좌표축은 세 개의 자식 즉, 두 개의 text 객체와 하나의 line 객체를 가지고 있습니다.

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.

그래픽스 객체 배열 사전할당(Preallocation)

MATLAB에서는 배열을 사용하기 전에 사전할당(Preallocation)하는 것이 모범 사례입니다. 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'