subtitle
说明
示例
添加副标题
创建一个绘图。使用 title
函数添加标题。然后使用 subtitle
函数添加副标题。
plot([0 2],[1 5]) title('Straight Line') subtitle('Slope = 2, y-Intercept = 1')
在副标题中包含变量
创建一个绘图,并向绘图添加标题。将 slopevalue
和 yintercept
定义为数值变量。将 txt
定义为字面文本的组合,并将 slopevalue
和 yintercept
的值转换为字符向量。然后,将 txt
传递给 subtitle
函数以显示副标题。
plot([0 2],[1 5]) title('Straight Line') slopevalue = 4; yintercept = 1; txt = ['Slope = ' int2str(slopevalue) ', y-Intercept = ' int2str(yintercept)]; subtitle(txt)
更改副标题颜色
创建一个绘图。使用 title
函数添加标题。然后,调用 subtitle
函数,并使用 'Color'
名称-值对组参数指定颜色。颜色可以是颜色名称,如 'red'
,您也可以使用 RGB 三元组或十六进制颜色代码指定自定义颜色。在本例中,指定 'red'
。
plot([0 2],[1 5]) title('Straight Line') subtitle('Slope = 2, y-Intercept = 1','Color','red')
或者,调用带输出参数的 subtitle
函数以返回文本对象。然后对文本对象设置颜色。在本例中,指定十六进制颜色代码 '#DD5500'
。
txt = subtitle('Plot of y = 2x + 1'); txt.Color = '#DD5500';
使用 TeX 标记创建多色副标题
创建一个绘图,并使用 title
函数添加标题。创建一个包含 TeX 标记的字符向量,为副标题中的不同单词使用自定义颜色。然后将该字符向量传递给 subtitle
函数。
plot([0 2],[1 5]) title('Straight Line') txt = ['An {\color{magenta}Attractive '... '\color[rgb]{0 .5 .5}and \color{red}Colorful} Subtitle']; subtitle(txt)
包括希腊符号
创建一个直方图,并使用 title
函数添加标题。创建一个包含使用希腊符号的 TeX 标记的字符向量。然后将该字符向量传递给 subtitle
函数。
histogram(5*randn(1,50)+10) title('Population Data') txt = '{\it\mu} = 10, {\it\sigma} = 5'; subtitle(txt)
包括上标和下标
创建一个直方图,并使用 title
函数添加标题。创建一个包含显示下标和上标的 TeX 标记的字符向量。然后将该字符向量传递给 subtitle
函数。
x = -10:0.1:10; y1 = x.^2; y2 = 2*x.^2; plot(x,y1,x,y2); title('Exponential Functions') txt = 'y_1 = x^2 and y_2 = 2x^{2 + k}'; subtitle(txt)
要为变量显示倾斜字体,请添加 \it
修饰符。
txt = '{\ity}_1 = {\itx}^2 and {\ity}_2 = 2{\itx}^{2 + \itk}';
subtitle(txt)
创建多行副标题
创建一个绘图,并使用 title
函数添加标题。然后通过将字符向量元胞数组传递给 subtitle
函数来创建包含两行文本的副标题。数组中的每个元素是一行文本。
plot([0 2],[1 5]) title('Straight Line') txt = {'Slope = 2','y-Intercept = 1'}; subtitle(txt)
按键入原样显示 TeX 字符
创建一个带有标题的绘图。然后创建一个包含下划线字符的副标题,TeX 解释器通常使用下划线字符表示下标。在调用 subtitle
函数时将 Interpreter
设置为 'none'
,以便下划线字符出现在副标题中。
plot([0 2],[1 5]) title('Straight Line') subtitle('y_1 = 2x + 1','Interpreter','none')
更改标题和副标题的对齐方式
创建一个绘图,并添加标题和副标题。获取当前坐标区,并通过将坐标区上的 TitleHorizontalAlignment
属性设置为 'left'
,将标题和副标题与图框的左边缘对齐。
plot([0 2],[1 5]) title('Straight Line') subtitle('Slope = 2, y-Intercept = 1') ax = gca; ax.TitleHorizontalAlignment = 'left';
通过将坐标区上的 TitleHorizontalAlignment
属性设置为 'center'
,使标题和副标题居中。
ax.TitleHorizontalAlignment = 'center';
指定目标坐标区
在一个分块图布局中创建两个绘图。然后向每个绘图加标题和副标题。
t = tiledlayout(1,2); % Left plot ax1 = nexttile; plot([0 2],[1 5]) title(ax1,'A Straight Line') subtitle(ax1,'Slope = 2, y-Intercept = 1') % Right plot ax2 = nexttile; plot([0 2],[2 8]) title(ax2,'Another Straight Line') subtitle(ax2,'Slope = 3, y-Intercept = 2')
输入参数
txt
— 副标题文本
字符向量 | 字符向量元胞数组 | 字符串数组
副标题文本,指定为字符向量、字符向量元胞数组或字符串数组。要创建多行文本,请指定一个字符向量元胞数组或字符串数组。
示例: subtitle('Single Line Subtitle')
示例: subtitle(["Subtitle With" "Multiple Lines"])
target
— 副标题的目标
坐标区 | 分块图布局 | 对象数组
副标题的目标,指定为下列项之一:
任何类型的坐标区:
Axes
、PolarAxes
或GeographicAxes
对象。TiledChartLayout
对象。由上述列表中的图形对象组成的数组。这些对象必须属于同一类。要确定类,请使用
class
函数。
如果不为副标题指定目标,则 subtitle
函数会将副标题添加到由 gca
命令返回的图形对象。
名称-值参数
将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参数名称,Value
是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: subtitle('My Subtitle','FontSize',12)
指定 12 磅字体大小。
注意
此处所列的属性只是一部分。有关完整列表,请参阅 Text 属性。
FontSize
— 字体大小
11
(默认) | 大于 0 的标量值
字体大小,指定为大于 0
的标量值(以磅为单位)。一磅等于 1/72 英寸。要更改字体单位,请使用 FontUnits
属性。
如果将标题或副标题添加到坐标区对象,则坐标区的字体大小属性也会影响标题和副标题的字体大小。标题和副标题字体大小是坐标区字体大小与一个缩放因子的乘积。坐标区的 FontSize
属性包含坐标区字体大小。坐标区的 TitleFontSizeMultiplier
属性包含缩放因子。默认情况下,坐标区的字体大小为 10 磅,缩放因子为 1.1,因此标题和副标题的字体大小各为 11 磅。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
FontWeight
— 文本字符的粗细
'normal'
(默认) | 'bold'
文本字符的粗细,指定为下列值之一:
'normal'
- 由特定字体定义的普通粗细'bold'
- 字符轮廓比普通粗
MATLAB® 使用 FontWeight
属性从系统提供的字体中选择一种字体。并非所有字体都有加粗字体。因此,指定加粗字体仍可能产生常规字体。
相关联坐标区的 SubtitleFontWeight
属性会影响副标题的 FontWeight
值。
提示
默认情况下,
Interactions
属性包含editInteraction
,因此可以通过点击文本来编辑文本。要禁用此交互,请将文本对象的Interactions
属性设置为[]
。
版本历史记录
在 R2020b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)