Main Content

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

legend

在坐标区上添加图例

说明

示例

legend 为每个绘制的数据序列创建一个带有描述性标签的图例。对于标签,图例使用数据序列的 DisplayName 属性中的文本。如果 DisplayName 属性为空,则图例使用 'dataN' 形式的标签。当您在坐标区上添加或删除数据序列时,图例会自动更新。此命令在由 gca 命令返回的当前坐标区中创建一个图例。如果当前坐标区为空,则图例为空。如果不存在坐标区,则 legend 创建一个笛卡尔坐标区。

示例

legend(label1,...,labelN) 设置图例标签。以字符向量或字符串列表形式指定标签,例如 legend('Jan','Feb','Mar')

legend(labels) 使用字符向量元胞数组、字符串数组或字符矩阵设置标签,例如 legend({'Jan','Feb','Mar'})

示例

legend(subset,___) 仅在图例中包括 subset 中列出的数据序列的项。subset 以图形对象向量的形式指定。您可以在指定标签之前或不指定其他输入参量的情况下指定 subset

示例

legend(target,___) 使用 target 指定的坐标区或独立可视化,而不是使用当前坐标区。指定 target 作为第一个输入参量。

示例

legend(___,'Location',lcn) 设置图例位置。例如,'Location','northeast' 将在坐标区的右上角放置图例。请在其他输入参量之后指定位置。

示例

legend(___,'Orientation',ornt)(其中 ornt'horizontal')并排显示图例项。ornt 的默认值为 'vertical',即垂直堆叠图例项。

示例

legend(___,Name,Value) 使用一个或多个名称-值对组参量来设置图例属性。

示例

legend(bkgd)(其中 bkgd'boxoff')删除图例背景和轮廓。bkgd 的默认值为 'boxon',即显示图例背景和轮廓。

lgd = legend(___) 返回 Legend 对象。可使用 lgd 在创建图例后查询和设置图例属性。有关属性列表,请参阅 Legend 属性

legend(vsbl) 控制图例的可见性,其中 vsbl'hide''show''toggle'

legend('off') 删除图例。

示例

全部折叠

绘制两个线条并在当前坐标区上添加一个图例。将图例标签指定为 legend 函数的输入参量。

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)

hold on 
y2 = cos(2*x);
plot(x,y2)

legend('cos(x)','cos(2x)')

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent cos(x), cos(2x).

如果您在坐标区添加或删除数据序列,图例会相应地更新。创建数据序列时,可通过将 DisplayName 属性设置为名称-值对组来控制新数据序列的标签。如果您不指定标签,则图例使用 'dataN' 形式的标签。

注意:如果您不希望在坐标区中添加或删除数据序列时自动更新图例,可将图例的 AutoUpdate 属性设置为 'off'

y3 = cos(3*x);
plot(x,y3,'DisplayName','cos(3x)')
hold off

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent cos(x), cos(2x), cos(3x).

删除图例。

legend('off')

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent cos(x), cos(2x), cos(3x).

从 R2019b 开始,您可以使用 tiledlayoutnexttile 函数显示分块图。调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1ax2。在每个坐标区中对随机数据绘图。通过指定 ax1 作为 legend 的第一个输入参量,在上部绘图中添加一个图例。

tiledlayout(2,1)
y1 = rand(3);
ax1 = nexttile; 
plot(y1)

y2 = rand(5);
ax2 = nexttile; 
plot(y2)

legend(ax1,{'Line 1','Line 2','Line 3'})

Figure contains 2 axes objects. Axes object 1 contains 3 objects of type line. These objects represent Line 1, Line 2, Line 3. Axes object 2 contains 5 objects of type line.

绘制两个线条。通过将 DisplayName 属性设置为所需的文本,在执行绘图命令的过程中指定图例标签。然后,添加一个图例。

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1,'DisplayName','cos(x)')

hold on 
y2 = cos(2*x);
plot(x,y2,'DisplayName','cos(2x)')
hold off

legend

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent cos(x), cos(2x).

要从图例中排除一个线条,请将其标签指定为空字符向量或字符串。例如,绘制两个正弦波,并通过调用 yline 函数在零位置添加一条水平虚线。然后创建一个图例,并通过将其标签指定为 '' 来排除该零位置的虚线。

x = 0:0.2:10;
plot(x,sin(x),x,sin(x+1));
hold on
yline(0,'--')
legend('sin(x)','sin(x+1)','')

Figure contains an axes object. The axes object contains 3 objects of type line, constantline. These objects represent sin(x), sin(x+1).

绘制四个线条。在坐标区的西北角创建一个图例。使用 NumColumns 属性指定图例的列数。

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)

hold on
y2 = cos(2*x);
plot(x,y2)

y3 = cos(3*x);
plot(x,y3)

y4 = cos(4*x);
plot(x,y4)
hold off

legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},...
    'Location','northwest','NumColumns',2)

Figure contains an axes object. The axes object contains 4 objects of type line. These objects represent cos(x), cos(2x), cos(3x), cos(4x).

默认情况下,图例项逐列从上到下排列。要逐行从左到右排列图例项,请将 Orientation 属性设置为 'horizontal'

自 R2023b 起

通过设置图例的 Direction 属性,可以反转图例项的顺序。例如,绘制四个线条并添加一个图例。

plot([4 5 6 7; 0 1 2 3])
lgd = legend("First","Second","Third","Fourth");

Figure contains an axes object. The axes object contains 4 objects of type line. These objects represent First, Second, Third, Fourth.

反转图例项的顺序。

lgd.Direction = "reverse";

Figure contains an axes object. The axes object contains 4 objects of type line. These objects represent First, Second, Third, Fourth.

当您要在两个或多个图之间共享一个图例时,您可以在布局的一个单独图块中显示该图例。您可以将图例放置在图块网格中,或放置在外侧图块中。

在一个分块图布局中创建三个绘图。

t = tiledlayout('flow','TileSpacing','compact');
nexttile
plot(rand(5))
nexttile
plot(rand(5))
nexttile
plot(rand(5))

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

添加一个共享图例,并将其移至第四个图块。

lgd = legend;
lgd.Layout.Tile = 4;

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

接下来,添加第四个绘图,并将图例移到 east 图块。

nexttile
plot(rand(5))
lgd.Layout.Tile = 'east';

Figure contains 4 axes objects. Axes object 1 contains 5 objects of type line. Axes object 2 contains 5 objects of type line. Axes object 3 contains 5 objects of type line. Axes object 4 contains 5 objects of type line.

如果您不想将绘制的所有图形对象都包含在图例中,可以指定要包含的图形对象。

绘制三个线条并返回创建的 Line 对象。创建只包含其中两条线的图例。将第一个输入参量指定为要包含的 Line 对象的向量。

x = linspace(0,pi);
y1 = cos(x);
p1 = plot(x,y1);

hold on
y2 = cos(2*x);
p2 = plot(x,y2);

y3 = cos(3*x);
p3 = plot(x,y3);
hold off

legend([p1 p3],{'First','Third'})

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent First, Third.

创建一个图,通过调用 legend 函数并将 Interpreter 属性设置为 'latex' 来添加包含 LaTeX 标记的图例。用美元符号 ($) 将标记括起来。

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

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent $sin(x)$, $\frac{d}{dx}sin(x)$.

绘制两个线条并创建一个图例。然后为图例添加标题。

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)

hold on
y2 = cos(2*x);
plot(x,y2)
hold off

lgd = legend('cos(x)','cos(2x)');
title(lgd,'My Legend Title')

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent cos(x), cos(2x).

绘制两个线条并在坐标区左下角创建一个图例。然后,删除图例的背景和轮廓。

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)

hold on
y2 = cos(2*x);
plot(x,y2)
hold off

legend({'cos(x)','cos(2x)'},'Location','southwest')
legend('boxoff')

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent cos(x), cos(2x).

您可以通过设置属性来更改图例的不同方面。您可以在调用 legend 时通过指定名称-值参量来设置属性,也可以在调用 legend 后设置 Legend 对象的属性。

绘制四行随机数据。创建一个图例并将 Legend 对象赋给变量 lgd。使用名称-值参量设置 FontSizeTextColor 属性。

rdm = rand(4);
plot(rdm)

lgd = legend({'Line 1','Line 2','Line 3','Line 4'},...
    'FontSize',12,'TextColor','blue');

Figure contains an axes object. The axes object contains 4 objects of type line. These objects represent Line 1, Line 2, Line 3, Line 4.

创建图例后,通过引用 lgd 来修改图例。使用对象圆点属性名称表示法设置 NumColumns 属性。

lgd.NumColumns = 2;

Figure contains an axes object. The axes object contains 4 objects of type line. These objects represent Line 1, Line 2, Line 3, Line 4.

输入参数

全部折叠

标签,指定为由字符向量或字符串组成的以逗号分隔的列表。

要从图例中排除某个项目,请将对应的标签指定为空字符向量或字符串。

要在标签中包括特殊字符或希腊字母,请使用 TeX 或 LaTeX 标记。要查看选项表,请参阅 Interpreter 属性。

要指定关键字标签,例如 'Location''off',请使用字符向量元胞数组、字符串数组或字符数组。

示例: legend('Sin Function','Cos Function')

示例: legend("Sin Function","Cos Function")

示例: legend("Sample A","","Sample C")

示例: legend('\gamma','\sigma')

标签,指定为字符向量元胞数组、字符串数组或分类数组。

要从图例中排除某个项目,请将对应的标签指定为元胞数组中的空字符向量或字符串数组中的空字符串。

要在标签中包括特殊字符或希腊字母,请使用 TeX 或 LaTeX 标记。要查看选项表,请参阅 Interpreter 属性。

示例: legend({'Sin Function','Cos Function'})

示例: legend(["Sin Function","Cos Function"])

示例: legend({'Sample A','','Sample C'})

示例: legend({'\gamma','\sigma'})

示例: legend(categorical({'Alabama','New York'}))

要包含在图例中的数据序列,指定为图形对象向量。

图例的目标,指定为 Axes 对象、PolarAxes 对象、GeographicAxes 对象或具有 LegendVisible 属性的独立可视化(例如 GeographicBubbleChart 对象)。如果不指定目标,则 legend 函数使用 gca 命令返回的对象作为目标。

独立可视化不支持修改图例外观(例如位置),或者不支持将 Legend 对象作为输出参量返回。

图例相对于坐标区的位置,指定为下表中列出的位置值之一。

描述
'north'坐标区中的顶部
'south'坐标区中的底部
'east'坐标区中的右侧区域
'west'坐标区中的左侧区域
'northeast'坐标区中的右上角(二维坐标区的默认值)
'northwest'坐标区中的左上角
'southeast'坐标区中的右下角
'southwest'坐标区中的左下角
'northoutside'坐标区的上方
'southoutside'坐标区的下方
'eastoutside'到坐标区的右侧
'westoutside'到坐标区的左侧
'northeastoutside'坐标区外的右上角(三维坐标区的默认值)
'northwestoutside'坐标区外的左上角
'southeastoutside'坐标区外的右下角
'southwestoutside'坐标区外的左下角
'best'创建图例时坐标区内与绘图数据冲突最小的位置。如果绘图数据发生变化,您可能需要将位置重置为 'best'
'bestoutside'坐标区的右上角之外(当图例为垂直方向时)或坐标区下方(当图例为水平方向时)
'layout'分块图布局中的图块。要将图例移至不同图块,请设置图例的 Layout 属性。
'none'Position 属性决定。可使用 Position 属性在自定义位置显示图例。

示例: legend('Location','northeastoutside')

方向,指定为下列值之一:

  • 'vertical' - 垂直堆叠图例项。

  • 'horizontal' - 并排列出图例项。

示例: legend('Orientation','horizontal')

图例框显示,指定为下列值之一:

  • 'boxon' - 显示图例背景和轮廓。

  • 'boxoff' - 不显示图例背景和轮廓。

示例: legend('boxoff')

图例可见性,指定为下列值之一:

  • 'hide' - 隐藏图例。

  • 'show' - 显示图例或创建图例(如果不存在)。

  • 'toggle' - 切换图例可见性。

示例: legend('hide')

名称-值参数

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

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

示例: legend({'A','B'},'TextColor','blue','FontSize',12) 创建一个蓝色、12 磅字体的图例。

注意

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

文本颜色,指定为 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

示例: [0 0 1]

示例: 'blue'

示例: '#0000FF'

字体大小,指定为大于 0 的标量值(以磅为单位)。默认字体大小取决于具体操作系统和区域设置。

如果您更改坐标区字体大小,MATLAB 会自动将颜色栏的字体大小设置为坐标区字体大小的 90%。如果手动设置了颜色栏的字体大小,则更改坐标区字体大小不会影响颜色栏的字体。

列数,指定为正整数。如果没有足够的图例项填充指定数量的列,则显示的列数可能会少一些。

使用 Orientation 属性控制图例项是逐列还是逐行依序显示。

示例: lgd.NumColumns = 3

输出参量

全部折叠

Legend 对象。可使用 lgd 在创建图例之后查看或修改图例属性。

plot(rand(3))
lgd = legend('line1','line2','line3');
lgd.FontSize = 12;
lgd.FontWeight = 'bold';

提示

  • 要为图例中 50 个以上的对象添加标签,必须为每个对象指定一个标签。否则,legend 仅描绘图中的前 50 个对象。

算法

  • 重新调用 legend 函数不会重置图例属性,如位置或方向。如果存在图例,legend 函数将更新现有图例。一个 Axes 对象仅可以拥有一个图例。

  • 图例反映图形对象在坐标区上的可见性。Visible 属性设置为 'off' 的图形对象在图例中灰显。

版本历史记录

在 R2006a 之前推出

全部展开

另请参阅

函数

属性