Main Content

向图中添加注释

注释是添加到图中的额外信息,用来帮助标识一些重要信息。此示例首先解释不同类型的注释,然后说明如何向图中添加圆圈和文本箭头。

注释的类型

使用 annotation 函数可以向图中添加注释。函数的第一个输入指定要创建的注释的类型。

  • 如果将类型指定为 'line''arrow''doublearrow''textarrow',则第二个输入是注释的起点和终点 x 位置。第三个输入是注释的起点和终点 y 位置。例如,annotation('line',[x_begin x_end],[y_begin y_end])

  • 如果将类型指定为 'rectangle''ellipse''textbox',则第二个参量是位置和大小。例如,annotation('rectangle',[x y w h])

注释使用归一化的图窗单位,并且可以在一个图窗中跨越多个坐标区。

创建简单的图

定义和绘制函数 f(x)g(x)

x = -3.0:0.01:3.0;
f = x.^2;
g = 5*sin(x) + 5;

figure
plot(x,f)    
hold on
plot(x,g)    
hold off

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

圆圈注释

在图中添加一个圆圈,以突出显示 f(x) 等于 g(x) 的位置。要创建圆圈,请使用 'ellipse' 选项作为注释类型。

通过设置底层对象的属性来自定义圆圈。将 Ellipse 对象作为 annotation 函数的输出参量返回。然后,使用圆点表示法访问对象的属性。例如,设置 Color 属性。

elps = annotation('ellipse',[.84 .68 .05 .05])
elps = 
  Ellipse with properties:

        Color: [0 0 0]
    FaceColor: 'none'
    LineStyle: '-'
    LineWidth: 0.5000
     Position: [0.8400 0.6800 0.0500 0.0500]
        Units: 'normalized'

  Use GET to show all properties

elps.Color = [0 0.5 0.5];

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

文本箭头注释

使用 'textarrow' 选项作为注释类型,向图中添加一个文本箭头。

您可以通过设置底层对象的属性来自定义文本箭头。将 TextArrow 对象作为 annotation 函数的输出参量返回。然后,使用圆点表示法访问对象的属性。例如,将 String 属性设置为所需的文本,将 Color 属性设置为颜色值。

ta = annotation('textarrow', [0.76 0.83], [0.71 0.71])
ta = 
  TextArrow with properties:

       String: {''}
     FontName: 'Helvetica'
     FontSize: 10
        Color: [0 0 0]
    TextColor: [0 0 0]
    LineStyle: '-'
    LineWidth: 0.5000
    HeadStyle: 'vback2'
     Position: [0.7600 0.7100 0.0700 0]
        Units: 'normalized'
            X: [0.7600 0.8300]
            Y: [0.7100 0.7100]

  Use GET to show all properties

ta.String = 'f(x) = g(x) ';              
ta.Color = [0 0.5 0.5];               

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

另请参阅

|

相关主题