Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

title

添加标题

说明

示例

title(titletext) 将指定的标题添加到当前坐标区或独立可视化中。重新发出 title 命令可使新标题替换旧标题。

示例

title(titletext,subtitletext) 在标题下添加副标题。

示例

title(___,Name,Value) 使用一个或多个名称-值对组参数修改标题外观。例如,'FontSize',12 将字体大小设置为 12 磅。在所有其他输入参数之后指定名称-值对组参数。并非所有图类型均支持修改标题外观。

示例

title(target,___) 将标题添加到指定的目标对象。

示例

t = title(___) 返回用于标题的对象。以后可以使用 t 修改标题。

示例

[t,s] = title(___) 返回用于标题和副标题的对象。使用 t 修改标题,使用 s 修改副标题。

示例

全部折叠

在当前坐标区中创建一个图窗并显示标题。

plot((1:10).^2)
title('My Title')

Figure contains an axes object. The axes object with title My Title contains an object of type line.

您也可以通过可返回文本的函数来调用 title。例如,date 函数返回包含今日日期的文本。

title(date)

Figure contains an axes object. The axes object with title 19-Aug-2023 contains an object of type line.

MATLAB® 将 date 的输出设置为坐标区的标题。

创建一个绘图。然后通过调用以两个字符向量作为参数的 title 函数创建一个和一个副标题。使用 'Color' 名称-值对组参数自定义两个文本行的颜色。指定两个返回参数来存储标题和副标题的文本对象。

plot([0 1])
[t,s] = title('Straight Line','Slope = 1, y-Intercept = 0',...
    'Color','blue');

Figure contains an axes object. The axes object with title Straight Line contains an object of type line.

将标题的字体大小更改为 16,并将副标题的字体角度更改为 'italic'

t.FontSize = 16;
s.FontAngle = 'italic';

Figure contains an axes object. The axes object with title Straight Line contains an object of type line.

在标题中包含变量 c 的值。

figure
plot((1:10).^2)
f = 70;
c = (f-32)/1.8;
title(['Temperature is ',num2str(c),' C'])

Figure contains an axes object. The axes object with title Temperature is 21.1111 C contains an object of type line.

使用颜色修饰符 \color 将其后尾随字符的颜色从先前的颜色更改为新颜色。

plot((1:10).^2)
title(['\fontsize{16}black {\color{magenta}magenta '...
'\color[rgb]{0 .5 .5}teal \color{red}red} black again'])

Figure contains an axes object. The axes object with title black blank magenta blank teal blank red black again contains an object of type line.

创建一个图,并将 t 定义为包含 LaTeX 标记的字符向量。用美元符号 ($) 将标记括起来。然后将 t 传递给 title 函数,并将 Interpreter 属性设置为 'latex'

x = 0:0.1:10;
y = sin(x);
dy = cos(x);
plot(x,y,x,dy);
t = '$sin(x)$ and $\frac{d}{dx}sin(x)$';
title(t,'interpreter','latex')

Figure contains an axes object. The axes object with title s i n leftParenthesis x rightParenthesis and StartFraction d over d x EndFraction s i n leftParenthesis x rightParenthesis contains 2 objects of type line.

使用 Name,Value 对组 'Color','m' 将标题的颜色设置为品红色。

figure
plot((1:10).^2)
title('Case number # 3','Color', 'm')

Figure contains an axes object. The axes object with title Case number # 3 contains an object of type line.

使用 TeX 标记在标题中包含希腊符号。

t = (0:0.01:0.2);
y = exp(-25*t);
figure
plot(t,y)
title('y = \ite^{\lambda t}','Color','b')

Figure contains an axes object. The axes object with title y = blank e toThePowerOf lambda blank t baseline contains an object of type line.

'Interpreter' 属性必须为 'tex'(默认值)。

figure
plot((1:10).^2)
title('\alpha^2 and X_1')

Figure contains an axes object. The axes object with title alpha Squared baseline and X_1 contains an object of type line.

上标字符“^”和下标字符“_”可修改紧随其后的花括号中定义的字符或子字符串。

使用多行元胞数组创建多行标题。

figure
plot((1:10).^2)
title({'First line';'Second line'})

Figure contains an axes object. The axes object with title First line Second line contains an object of type line.

Interpreter 属性设置为 'none',以使文本 X_1 按键入原样显示在图窗中,而不会将 1 设置为 X 的下标。

plot((1:10).^2)
title('X_1','Interpreter','none')

Figure contains an axes object. The axes object with title X_1 contains an object of type line.

从 R2019b 开始,您可以使用 tiledlayoutnexttile 函数显示分块图。调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1ax2。然后将数据绘制到每个坐标区中。

tiledlayout(2,1)
ax1 = nexttile;
plot(ax1,(1:10).^2)
ax2 = nexttile;
plot(ax2,(1:10).^3)

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

通过将 ax1ax2 传递给 title 函数,为每个坐标区添加标题。

title(ax1,'Top Plot')
title(ax2,'Bottom Plot')

Figure contains 2 axes objects. Axes object 1 with title Top Plot contains an object of type line. Axes object 2 with title Bottom Plot contains an object of type line.

在绘图上添加标题,并返回文本对象。

plot((1:10).^2)
t = title('My Title');

Figure contains an axes object. The axes object with title My Title contains an object of type line.

将标题的颜色设置为红色。使用圆点表示法设置属性。

t.Color = 'red';

Figure contains an axes object. The axes object with title My Title contains an object of type line.

创建一个带有标题的绘图。获取当前坐标区,然后通过将坐标区的 TitleHorizontalAlignment 属性设置为 'left',将标题与图框的左边缘对齐。

plot([0 2],[1 5])
title('Straight Line')
ax = gca;
ax.TitleHorizontalAlignment = 'left';

Figure contains an axes object. The axes object with title Straight Line contains an object of type line.

通过将坐标区上的 TitleHorizontalAlignment 属性设置为 'center' 使标题居中。

ax.TitleHorizontalAlignment = 'center';

Figure contains an axes object. The axes object with title Straight Line contains an object of type line.

输入参数

全部折叠

标题文本,指定为字符向量、字符向量元胞数组、字符串数组、分类数组或数值。根据您需要的文本外观指定一个值。下表列出了最常见的情况。

文本外观值的描述示例
一行文本 字符向量或字符串标量。

txt = 'My Text'
txt = "My Text"

多行文本字符向量元胞数组或字符串数组。

txt = {'First line','Second line'};
txt = ["First line", "Second line"];

包含数值变量的文本包含已转换为 char 数组的数值的数组。使用 num2str 转换值。

x = 42;
txt = ['The value is ',num2str(x)];

包含特殊字符(例如希腊字母或数学符号)的文本包含 TeX 标记的数组。有关支持的标记的列表,请参阅 Interpreter 属性。

txt = 'x ranges from 0 to 2\pi'

注意

  • defaultfactoryremove 是保留字,当作为标准字符引用时不显示在文本中。要在文本中单独显示这些单词中的任意单词,需在该单词之前放置反斜杠,例如 '\default''\remove'

  • 如果将此属性指定为分类数组,MATLAB® 将使用数组中的值,而不是类别。

  • 如果您指定仅包含数值的文本,此数值将会使用 sprintf('%g',value) 转换。例如,12345678 显示为 1.23457e+07

副标题文本,指定为字符向量、字符向量元胞数组、字符串数组、分类数组或数值。根据您需要的文本外观指定一个值。下表列出了最常见的情况。

文本外观值的描述示例
一行文本 字符向量或字符串标量。

txt = 'My Text'
txt = "My Text"

多行文本字符向量元胞数组或字符串数组。

txt = {'First line','Second line'};
txt = ["First line", "Second line"];

包含数值变量的文本包含已转换为 char 数组的数值的数组。使用 num2str 转换值。

x = 42;
txt = ['The value is ',num2str(x)];

包含特殊字符(例如希腊字母或数学符号)的文本包含 TeX 标记的数组。有关支持的标记的列表,请参阅 Interpreter 属性。

txt = 'x ranges from 0 to 2\pi'

注意

  • defaultfactoryremove 是保留字,当作为标准字符引用时不显示在文本中。要在文本中单独显示这些单词中的任意单词,需在该单词之前放置反斜杠,例如 '\default''\remove'

  • 如果将此属性指定为分类数组,MATLAB 将使用数组中的值,而不是类别。

  • 如果您指定仅包含数值的文本,此数值将会使用 sprintf('%g',value) 转换。例如,12345678 显示为 1.23457e+07

要添加标题的目标对象,指定为以下项之一:

  • 任何类型的坐标区:AxesPolarAxesGeographicAxes 对象。

  • 任何类型的图例:LegendBubbleLegend 对象。

  • TiledChartLayout 对象。

  • 具有 Title 属性的独立可视化。例如,您可以向 HeatmapChart 对象添加标题。

  • 由上述列表中的图形对象组成的数组。这些对象必须属于同一类。要确定类,请使用 class 函数。

如果不为标题指定目标,则 title 函数会将标题添加到由 gca 命令返回的图形对象。

注意

有些图不支持修改标题外观(例如颜色),或以输出参数形式返回文本对象。

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: 'Color','red','FontSize',14 将添加一个红色、14 磅字体的标题。

除了以下属性之外,还可以使用 Name,Value 对组参数指定其他文本对象属性。请参阅 Text 属性

字体大小,指定为大于 0 的标量值(以磅为单位)。一磅等于 1/72 英寸。要更改字体单位,请使用 FontUnits 属性。

如果将标题或副标题添加到坐标区对象,则坐标区的字体大小属性也会影响标题和副标题的字体大小。标题和副标题字体大小是坐标区字体大小与一个缩放因子的乘积。坐标区的 FontSize 属性包含坐标区字体大小。坐标区的 TitleFontSizeMultiplier 属性包含缩放因子。默认情况下,坐标区的字体大小为 10 磅,缩放因子为 1.1,因此标题和副标题的字体大小各为 11 磅。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

文本字符的粗细,指定为下列值之一:

  • 'normal' - 由特定字体定义的普通粗细

  • 'bold' - 字符轮廓比普通粗

MATLAB 使用 FontWeight 属性从系统提供的字体中选择一种字体。并非所有字体都有加粗字体。因此,指定加粗字体仍可能产生常规字体。

相关联坐标区的 TitleFontWeight 属性影响标题的 FontWeight 值。同样,相关联坐标区的 SubtitleFontWeight 属性影响副标题的 FontWeight 值。

注意

'light''demi' 字体粗细值已被移除。请改用 'normal'

字体名称,指定为支持的字体名称或 "FixedWidth"。要正确显示和打印文本,您必须选择系统支持的字体。默认字体取决于您的操作系统和区域设置。

要使用在任何区域设置中都有较好显示效果的等宽字体,请使用 "FixedWidth"。等宽字体依赖于根 FixedWidthFontName 属性。设置根 FixedWidthFontName 属性会导致立即更新显示方式以使用新字体。

文本颜色,指定为 RGB 三元组、十六进制颜色代码、颜色名称或短名称。默认值 [0 0 0] 对应于黑色。

对于自定义颜色,请指定 RGB 三元组或十六进制颜色代码。

  • RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于 [0,1] 范围内,例如 [0.4 0.6 0.7]

  • 十六进制颜色代码是字符串标量或字符向量,以井号 (#) 开头,后跟三个或六个十六进制数字,范围可以是 0F。这些值不区分大小写。因此,颜色代码 "#FF8800""#ff8800""#F80""#f80" 是等效的。

此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。

颜色名称短名称RGB 三元组十六进制颜色代码外观
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

"none"不适用不适用不适用无颜色

以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。

RGB 三元组十六进制颜色代码外观
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

示例: 'blue'

示例: [0 0 1]

示例: '#0000FF'

文本解释器,指定为下列值之一:

  • 'tex' - 使用 TeX 标记子集解释字符。

  • 'latex' - 使用 LaTeX 标记解释字符。

  • 'none' - 显示字面字符。

TeX 标记

默认情况下,MATLAB 支持一部分 TeX 标记。使用 TeX 标记可添加下标和上标,修改字体类型和颜色,并在文本中包括特殊字符。

修饰符会一直作用到文本结尾,但上标和下标除外,因为它们仅修饰下一个字符或花括号中的字符。当您将解释器设置为 'tex' 时,支持的修饰符如下所示。

修饰符描述示例
^{ }上标'text^{superscript}'
_{ }下标'text_{subscript}'
\bf粗体'\bf text'
\it斜体'\it text'
\sl伪斜体(通常与斜体相同)'\sl text'
\rm常规字体'\rm text'
\fontname{specifier}字体名称 - 将 specifier 替换为字体系列的名称。您可以将此说明符与其他修饰符结合使用。'\fontname{Courier} text'
\fontsize{specifier}字体大小 - 将 specifier 替换为以磅为单位的数值标量值。'\fontsize{15} text'
\color{specifier}字体颜色 - 将 specifier 替换为以下颜色之一:redgreenyellowmagentablueblackwhitegraydarkGreenorangelightBlue'\color{magenta} text'
\color[rgb]{specifier}自定义字体颜色 - 将 specifier 替换为三元素 RGB 三元组。'\color[rgb]{0,0.5,0.5} text'

下表列出了 'tex' 解释器所支持的特殊字符。

字符序列符号字符序列符号字符序列符号

\alpha

α

\upsilon

υ

\sim

~

\angle

\phi

ϕ

\leq

\ast

*

\chi

χ

\infty

\beta

β

\psi

ψ

\clubsuit

\gamma

γ

\omega

ω

\diamondsuit

\delta

δ

\Gamma

Γ

\heartsuit

\epsilon

ϵ

\Delta

Δ

\spadesuit

\zeta

ζ

\Theta

Θ

\leftrightarrow

\eta

η

\Lambda

Λ

\leftarrow

\theta

θ

\Xi

Ξ

\Leftarrow

\vartheta

ϑ

\Pi

Π

\uparrow

\iota

ι

\Sigma

Σ

\rightarrow

\kappa

κ

\Upsilon

ϒ

\Rightarrow

\lambda

λ

\Phi

Φ

\downarrow

\mu

µ

\Psi

Ψ

\circ

º

\nu

ν

\Omega

Ω

\pm

±

\xi

ξ

\forall

\geq

\pi

π

\exists

\propto

\rho

ρ

\ni

\partial

\sigma

σ

\cong

\bullet

\varsigma

ς

\approx

\div

÷

\tau

τ

\Re

\neq

\equiv

\oplus

\aleph

\Im

\cup

\wp

\otimes

\subseteq

\oslash

\cap

\in

\supseteq

\supset

\lceil

\subset

\int

\cdot

·

\o

ο

\rfloor

\neg

¬

\nabla

\lfloor

\times

x

\ldots

...

\perp

\surd

\prime

´

\wedge

\varpi

ϖ

\0

\rceil

\rangle

\mid

|

\vee

\langle

\copyright

©

LaTeX 标记

要使用 LaTeX 标记,请将解释器设置为 'latex'。对于行内模式,用单美元符号 ($) 将标记括起来。对于显示模式,用双美元符号 ($$) 将标记括起来。

LaTeX 模式示例结果
行内

'$\int_1^{20} x^2 dx$'

Equation with LaTeX inline mode

显示

'$$\int_1^{20} x^2 dx$$'

Equation with LaTeX display mode

显示的文本将使用默认的 LaTeX 字体样式。FontNameFontWeightFontAngle 属性不起任何作用。要更改字体样式,请使用 LaTeX 标记。

用于 LaTeX 解释器的文本最多可以包含 1200 个字符。对于多行文本,请在此数量的基础上每行减少约 10 个字符。

有关使用 TeX 和 LaTeX 的示例,请参阅图文本中的希腊字母和特殊字符。有关 LaTeX 系统的详细信息,请访问 LaTeX Project 网站:https://www.latex-project.org/

输出参数

全部折叠

用于标题的对象,返回下列对象类型之一:

  • 文本对象 - 如果您为坐标区添加标题,title 将返回文本对象。创建标题后,可以使用此文本对象访问和修改标题属性。有关文本对象属性的列表,请参阅 Text 属性。您还可以通过坐标区对象的 Title 属性访问标题。

  • 图例文本对象 - 如果您为图例添加标题,title 将返回图例文本对象。创建标题后,可以使用此图例文本对象访问和修改标题属性。有关图例文本对象属性的列表,请参阅 Text 属性。您还可以通过图例对象的 Title 属性访问标题。

用于副标题的对象,以文本对象形式返回。创建副标题后,使用此文本对象修改副标题的属性。有关文本对象属性的列表,请参阅 Text 属性。您还可以通过坐标区对象的 Subtitle 属性访问副标题。

提示

  • 默认情况下,Interactions 属性包含 editInteraction,因此可以通过点击文本来编辑文本。要禁用此交互,请将文本对象的 Interactions 属性设置为 []

版本历史记录

在 R2006a 之前推出