Main Content

向图中添加文本

此示例说明如何向图中添加文本、控制文本位置和大小以及创建多行文本。

文本位置

使用 text 函数在特定数据点旁边添加文本。在本例中,为点 (π,sin(π)) 添加文本。text 函数的前两个输入参量指定位置。第三个参量指定了文本。

默认情况下,text 支持一部分 TeX 标记。使用 TeX 标记 \pi 表示希腊字母 π。通过包含 TeX 标记 \leftarrow,显示一个指向左侧的箭头。有关标记的完整列表,请参阅图文本中的希腊字母和特殊字符

x = linspace(0,10,50);
y = sin(x);
plot(x,y)

txt = '\leftarrow sin(\pi) = 0';
text(pi,sin(pi),txt)

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

文本对齐方式

默认情况下,指定的数据点位于文本的左侧。通过将 HorizontalAlignment 属性指定为 'right',使数据点出现在文本右侧。使用指向右侧而不是左侧的箭头。

x = linspace(0,10,50);
y = sin(x);
plot(x,y)

txt = 'sin(\pi) = 0 \rightarrow';
text(pi,sin(pi),txt,'HorizontalAlignment','right')

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

字体大小

通过将 FontSize 属性设置为 text 函数的名称-值对组参量,指定文本的字体大小。使用 titlexlabelylabellegend 函数时,可以通过类似的方法更改字体大小。

x = linspace(0,10,50);
y = sin(x);
plot(x,y)

txt = '\leftarrow sin(\pi) = 0';
text(pi,sin(pi),txt,'FontSize',14)

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

从 R2022a 开始,您也可以使用 fontsize 函数更改坐标区文本的字体大小。

设置文本属性

text 函数用于创建 Text 对象。Text 对象具有可用来自定义文本外观的属性,例如 HorizontalAlignmentFontSize

您可以通过两种方式设置属性:

  • text 命令中使用名称-值对组,例如 'FontSize',14

  • 使用 Text 对象。您可以将 Text 对象作为 text 函数的输出参量返回,并将其赋给某个变量,例如 t。然后,使用圆点表示法设置属性,例如 t.FontSize = 14

对于此示例,使用圆点表示法而不是名称-值对组来更改字体大小。

x = linspace(0,10,50);
y = sin(x);
plot(x,y)

txt = '\leftarrow sin(\pi) = 0';
t = text(pi,sin(pi),txt)
t = 
  Text (\leftarrow sin(\pi) = 0) with properties:

                 String: '\leftarrow sin(\pi) = 0'
               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

t.FontSize = 14;

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

多行文本

使用字符向量元胞数组显示跨越多行的文本。元胞数组的每个元素代表一行文本。对于此示例,显示包含两行的标题。使用 titlexlabelylabellegend 函数时,可以通过类似的方法显示多行文本。

x = linspace(0,10,50);
y = sin(x);
plot(x,y)

txt = {'Plotted Data:','y = sin(x)'};
text(4,0.5,txt)

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

带有变量值的文本

通过使用 num2str 函数将数字转换为文本,可在文本中包含变量值。对于此示例,计算均值 y 并在标题中包含该值。使用 titlexlabelylabellegend 函数时,可以通过类似的方法包含变量值。

x = linspace(0,10,50);
y = sin(x);
plot(x,y)

avg = mean(y);
txt = ['Average height: ' num2str(avg) ' units'];
text(4,0.5,txt)

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

坐标区外部的文本

使用 annotation 函数而不是 text 函数,可在图窗内的任何位置添加文本。第一个输入参量指定注释的类型。第二个输入参量以归一化的图窗单位指定注释的位置。通过将 EdgeColor 属性设置为 'none',删除文本框边框。有关文本框注释的详细信息,请参阅 annotation 函数。

x = linspace(0,10,50);
y = sin(x);
plot(x,y)

annotation('textbox',[.9 .5 .1 .2], ...
    'String','Text outside the axes','EdgeColor','none')

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

另请参阅

| | | | |

相关主题