Main Content

xline

具有常量 x 值的垂直线

说明

示例

xline(x) 在当前坐标区的一个或多个 x 坐标处创建一条垂直线。例如,xline(2)x=2 处创建一条线。

示例

xline(x,LineSpec) 指定线型、线条颜色或同时指定两者。例如,xline([12 20 33],'--b') 创建三条蓝色虚线。

示例

xline(x,LineSpec,labels) 为线条添加标签。

示例

xline(___,Name,Value) 使用一个或多个名称-值对组参量指定常量线属性。有关属性列表,请参阅 ConstantLine 属性。请在所有其他输入参量之后指定名称-值对组。

示例

xline(ax,___) 在指定的坐标区(而不是当前坐标区)中创建线条。

示例

xl = xline(___) 返回一个 ConstantLine 对象或 ConstantLine 对象数组。使用 xl 修改或查询所创建的线条的属性。

示例

全部折叠

在 x = 3 处创建一条垂直线。

xline(3);

要创建一条具有标签的线条,还必须指定线型。默认线型是实线 '-'

x = linspace(0,6,100);
y = exp(x);
plot(x,y)
xline(4.5,'-',{'Acceptable','Limit'});

创建一个由正态分布随机数组成的向量,并在直方图中显示它们。然后创建三条带标签的垂直线,一条表示平均值、一条表示低于平均值一个标准差,一条表示高于平均值一个标准差。

data = 5 * randn(1,500) + 20;
histogram(data,'FaceAlpha',0.1,'EdgeAlpha',0.1);
m = mean(data);
s = std(data);
xline([m-s m m+s],'-',{'-1 Standard Dev.','Average','+1 Standard Dev.'})

在 x = 5 处创建一条红色虚线。

xline(5,'--r');

为图例创建一条既具有标签又具有显示名称的垂直点划线。

y = [3187 2693 1771 1826 1958 3222 1645];
barh(y,'DisplayName','Daily Sales')
xl = xline(2328,'-.','Average','DisplayName','Average Sales');

调整该标签的垂直和水平对齐,然后显示图例。

xl.LabelVerticalAlignment = 'middle';
xl.LabelHorizontalAlignment = 'center';
legend('show');

调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1ax2。然后在每个坐标区中绘图。通过将坐标区传递给 xline 函数,为每个图添加一条垂直虚线和标签。

tiledlayout(2,1)
ax1 =nexttile;
x = linspace(0,10,200);
y1 = cos(x); 
plot(ax1,x,y1)

ax2 = nexttile;
y2 = sin(x); 
plot(ax2,x,y2)

xline(ax1,pi/2,':','cos(\pi/2)=0');
xline(ax2,pi,':','sin(\pi)=0');

输入参数

全部折叠

x 坐标,指定为标量或包含数值、分类、日期时间或持续时间值的向量。x 的数据类型必须与 x 轴的数据类型匹配。

示例: xline(10)x=10 处显示一条线。

示例: xline([13 20 33])x=13x=20x=33 处显示线条。

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

线型和颜色,指定为包含字符和符号的字符向量或字符串标量。字符和符号可以按任何顺序出现。您可以指定线型和/或线条颜色。忽略 'o' 等标记符号。

示例: '--g' 表示绿色虚线。

线型描述表示的线条
"-"实线

Sample of solid line

"--"虚线

Sample of dashed line

":"点线

Sample of dotted line

"-."点划线

Sample of dash-dotted line, with alternating dashes and dots

颜色名称短名称外观
'red''r'

Sample of the color red

'green''g'

Sample of the color green

'blue''b'

Sample of the color blue

'cyan' 'c'

Sample of the color cyan

'magenta''m'

Sample of the color magenta

'yellow''y'

Sample of the color yellow

'black''k'

Sample of the color black

'white''w'

Sample of the color white

目标坐标区,指定为 Axes 对象。如果希望 xline 在当前坐标区以外的坐标区中绘图,请使用此参量。

信号线标签,指定为字符向量、字符串标量、字符向量元胞数组或字符串数组。

为一条线创建标签

指定一个字符向量或字符串标量来显示一行文本。要显示多行文本,请指定字符向量元胞数组或字符串数组。

表示形式如何指定 labels示例
一行文本指定一个字符向量或字符串标量。

xline(12,'-','Sample')

A vertical line in an axes with a label

多行文本指定一个字符向量元胞数组或字符串数组。数组中的每个元素是不同行的文本。
xline(12,'-',{'Sample','x=12'})

A vertical line in an axes with a label that has two lines of text

为多行创建标签

指定一个字符向量或字符串标量,以便在所有行旁边显示相同的标签。指定一个字符向量元胞数组或字符串数组,为每行显示不同标签。

表示形式如何指定 labels示例
一个共享文本标签指定一个字符向量或字符串标量。

xline([13 20 33],'-','Sample')

Three vertical lines in an axes with matching labels

每行有不同标签指定一个字符向量元胞数组或字符串数组。数组中的每个元素都是一个对应不同行的标签。labels 数组中的元素数必须与 x 的长度匹配。
label = {'Sample 1','Sample 2','Sample 3'};
xline([13 20 33],'-',label)

Three vertical lines in an axes with different labels

不同的标签,部分标签有多行创建一个字符向量元胞数组或字符串数组。使用 sprintf 函数在文本中插入换行符 ('\n')。
label = {'Sample 1',sprintf('Sample 2\nx=20'),'Sample 3'};
xline([13 20 33],'-',label)

Three vertical lines in an axes with different labels. The label for the second line has two lines of text.

名称-值参数

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

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

示例: xline(5,'LabelVerticalAlignment','middle') 将标签的垂直对齐方式指定为 'middle'

注意

此处所列的属性只是一部分。有关完整列表,请参阅 ConstantLine 属性

线条颜色,指定为 RGB 三元组、十六进制颜色代码、颜色名称或短名称。

对于自定义颜色,请指定 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

示例: 'g'

示例: [0.6 0.2 0.5]

示例: '#D2F9A7'

线宽,指定为以磅为单位的正值。

图例标签,指定为字符向量或字符串标量。只有调用 legend 命令之后,才会显示图例。如果未指定文本,则 legend 使用 'dataN' 形式设置标签。

标签相对于线的水平对齐,指定为下表中的选项之一。

选项描述示例
'right'线的右侧。

Vertical line with the label to the right of the line

'left'线的左侧。

Vertical line with the label to the left of the line

'center'线的中心。标签将线分段。

Vertical line with the label centered with the line

标签相对于线的垂直对齐,指定为下表中的选项之一。

选项描述示例
'top'线的顶部

Vertical line with the label at the top of the line

'middle'线的中间

Vertical line with the label vertically centered

'bottom'线的底部

Vertical line with the label at the bottom of the line

标签方向,指定为 'aligned''horizontal'。下表中显示了示例。

方向描述示例
'aligned'标签与线的方向相同。

Vertical line with a vertical label

'horizontal'无论线的方向如何,标签始终为水平方向。

Vertical line with a horizontal label

算法

在坐标区的三维视图中,常量线出现在 z 坐标轴范围中点处的 x-y 平面中。如果坐标区旋转,则常量线会随之旋转。

版本历史记录

在 R2018b 中推出

另请参阅

函数

属性