matlab 多个figure下修改某一副_MATLAB作图实例:63:修改图形对象

403af8f095a7d873c4d82b8f961678de.png

本示例说明如何在MATLAB中创建、显示和修改图形对象。

图形对象

MATLAB创建绘图时,将创建一系列图形对象。图形、轴、线条、贴片和文本是图形对象。下图具有三个图形对象:轴、线条和文本。使用可选的输出参数来存储创建的图形对象。

 x = -pi:pi/20:pi;
 y = sin(x);
 ​
 f = figure;
 p = plot(x,y);
 txt1 = text(0.2,0,'sin(x)');

91eb47fb12bd66c0515057efc4cbaf34.png

所有图形对象都具有可以查看和修改的属性。这些属性具有默认值。行对象p,显示最常用的线条属性,如ColorLineStyle,和LineWidth

 p
 p = 
   Line with properties:
 ​
               Color: [0 0.4470 0.7410]
           LineStyle: '-'
           LineWidth: 0.5000
              Marker: 'none'
          MarkerSize: 6
     MarkerFaceColor: 'none'
               XData: [1x41 double]
               YData: [1x41 double]
               ZData: [1x0 double]
 ​
   Show all properties

如果创建对象的命令中没有分号,那么MATLAB会有相同的显示。

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

c5636add0abf5ad15de33c9107c2b0c6.png
 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

获取图形对象属性

要访问单个图形对象的属性,请使用点符号语法:object.PropertyName。例如,返回线条对象LineWidth的属性。

 pcol = p.LineWidth
 pcol = 0.5000

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

 p.Color = 'red';

44cc47441f12eda23e0a6c9c4c0ae7ec.png

对象层级

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'
 ​
   Show all properties

所有图形对象(根除外)都有一个父级。例如,轴的父级是图形。

 ax = gca;
 ax.Parent
 ans = 
   Figure (1) with properties:
 ​
       Number: 1
         Name: ''
        Color: [0.9400 0.9400 0.9400]
     Position: [348 480 583 437]
        Units: 'pixels'
 ​
   Show all properties

许多对象也有子层级。该轴具有三个子对象:两个文本对象和线条对象。

 ax.Children
 ans = 
   3x1 graphics array:
 ​
   Text    (pi)
   Text    (sin(x))
   Line

由于轴具有多个子层级,因此Children属性的值是图形对象的数组。要访问轴的单个子级,请索引到数组。然后,我们可以设置子对象的属性。

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

d361f60472923c66dee2fb9002456c57.png

预分配图形对象数组

在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: [1x64 double]
              BeingDeleted: off
                BusyAction: 'queue'
             ButtonDownFcn: ''
                  Children: [1x1 Axes]
                  Clipping: on
           CloseRequestFcn: 'closereq'
                     Color: [0.9400 0.9400 0.9400]
                  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'
             InnerPosition: [348 480 583 437]
             IntegerHandle: on
             Interruptible: on
            InvertHardcopy: on
               KeyPressFcn: ''
             KeyReleaseFcn: ''
                   MenuBar: 'none'
                      Name: ''
                  NextPlot: 'add'
                    Number: 1
               NumberTitle: on
             OuterPosition: [344 476 591 464]
          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 480 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: ':100'

注:本文根据MATLAB官网内容修改而成。

我推荐给你以下的视频教程,特点是没有PPT,不掺水,直接编程环境下的实操课程: 用360分钟了解MATLAB编程《MATLAB编程360》视频课程:

MATLAB编程360 - 网易云课堂study.163.com​study.163.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值