This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Graphics Objects

This example shows how to create, display, and modify graphics objects in MATLAB®.

Graphics Objects

When MATLAB creates a plot, it creates a series of graphics objects. Figures, axes, lines, patches, and text are examples of graphics objects. The figure below has three graphics objects -- an axes, a line, and a text object. Use an optional output argument to store the graphics object that is created.

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

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

All graphics objects have properties that you can view and modify. These properties have default values. The display of the line object, p, shows the most commonly used line properties, such as Color, LineStyle, and LineWidth.

p = 
  Line with properties:

              Color: [0 0.4470 0.7410]
          LineStyle: '-'
          LineWidth: 0.5000
             Marker: 'none'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: [1×41 double]
              YData: [1×41 double]
              ZData: [1×0 double]

  Show all properties

MATLAB shows the same display if the semicolon is missing from the command that creates the object.

txt2 = text(x(end), y(end), 'pi')
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'

  Show all properties

Get Graphics Object Properties

To access individual properties of a graphics object, use dot notation syntax object.PropertyName. For example, return the LineWidth property for the line object.

pcol = p.LineWidth
pcol = 0.5000

Change the line color to red by setting its Color property.

p.Color = 'red';

Parents and Children

MATLAB arranges graphics objects in a hierarchy. The top of the hierarchy is a special object called the graphics root. To access the graphics root, use the groot function.

ans = 
  Graphics Root with properties:

          CurrentFigure: [1×1 Figure]
    ScreenPixelsPerInch: 75
             ScreenSize: [1 1 1280 1024]
       MonitorPositions: [1 1 1280 1024]
                  Units: 'pixels'

  Show all properties

All graphics objects (except the root) have a parent. For example, the parent of an axes is a figure.

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

      Number: 1
        Name: ''
       Color: [0.9400 0.9400 0.9400]
    Position: [360 502 560 420]
       Units: 'pixels'

  Show all properties

Many objects also have children. This axes has three children - the two text objects and the line object.

ans = 
  3×1 graphics array:

  Text    (pi)
  Text    (sin(x))

Since the axes has multiple children, the value of the Children property is an array of graphics objects. To access an individual child of the axes, index into the array. You can then set properties of the child object.

t = ax.Children(2);       % get the 'sin(x)' text object
t.FontWeight = 'bold';    % set the font to bold

Preallocate Graphics Objects Array

It is a best practice in MATLAB to preallocate an array before using it. Use the gobjects command to preallocate an array of graphics objects. You can then add graphics objects to the array.

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

    Figure    Axes      Line      Text      Text  

Get All Object Properties

Graphics objects in MATLAB have many properties. To see all the properties of an object, use the get command.

                 Alphamap: [1×64 double]
             BeingDeleted: 'off'
               BusyAction: 'queue'
            ButtonDownFcn: ''
                 Children: [1×1 Axes]
                 Clipping: 'on'
          CloseRequestFcn: 'closereq'
                    Color: [0.9400 0.9400 0.9400]
                 Colormap: [64×3 double]
                CreateFcn: ''
              CurrentAxes: [1×1 Axes]
         CurrentCharacter: ''
            CurrentObject: [0×0 GraphicsPlaceholder]
             CurrentPoint: [0 0]
                DeleteFcn: ''
             DockControls: 'on'
                 FileName: ''
        GraphicsSmoothing: 'on'
         HandleVisibility: 'on'
            InnerPosition: [360 502 560 420]
            IntegerHandle: 'on'
            Interruptible: 'on'
           InvertHardcopy: 'on'
              KeyPressFcn: ''
            KeyReleaseFcn: ''
                  MenuBar: 'none'
                     Name: ''
                 NextPlot: 'add'
                   Number: 1
              NumberTitle: 'on'
            OuterPosition: [353 495 574 453]
         PaperOrientation: 'portrait'
            PaperPosition: [0.5167 2.7000 7.4667 5.6000]
        PaperPositionMode: 'auto'
                PaperSize: [8.5000 11]
                PaperType: 'usletter'
               PaperUnits: 'inches'
                   Parent: [1×1 Root]
                  Pointer: 'arrow'
        PointerShapeCData: [16×16 double]
      PointerShapeHotSpot: [1 1]
                 Position: [360 502 560 420]
                 Renderer: 'opengl'
             RendererMode: 'auto'
                   Resize: 'on'
            SelectionType: 'normal'
           SizeChangedFcn: ''
                      Tag: ''
                  ToolBar: 'none'
                     Type: 'figure'
            UIContextMenu: [0×0 GraphicsPlaceholder]
                    Units: 'pixels'
                 UserData: []
                  Visible: 'off'
      WindowButtonDownFcn: ''
    WindowButtonMotionFcn: ''
        WindowButtonUpFcn: ''
        WindowKeyPressFcn: ''
      WindowKeyReleaseFcn: ''
     WindowScrollWheelFcn: ''
              WindowStyle: 'normal'
                 XDisplay: ':0'
Was this topic helpful?