Main Content

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

highlight

突出显示绘制的图中的节点和边

说明

示例

highlight(H,nodeIDs) 通过增加由 nodeIDs 指定的节点的标记大小突出显示这些节点。

示例

highlight(H,G) 通过分别增加图 G 的节点标记大小和边线宽,突出显示这些节点和边。G 必须具有与基础图 H 相同的节点,但具有的边是后者具有的边的子集。如果 G 包含重复的边,这些边都会突出显示。度数为 0 的孤立节点不会突出显示。

highlight(H,s,t) 通过增加边的线宽来突出显示 st 中指定的源节点和目标节点对之间的所有边。如果知道边索引而不是节点对 (s,t),则可以改用 highlight(H,'Edges',idx)

示例

highlight(___,Name,Value) 使用一个或多个名称-值对组参数(采用上述语法中的任意输入参数组合)指定的其他选项。例如,highlight(H,nodes,'NodeColor','g') 通过将部分节点的颜色更改为绿色(而不是增加其标记大小)突出显示这些节点。

示例

全部折叠

创建并绘制一个图。返回 GraphPlot 对象的句柄 h。

s = 1;
t = 2:6;
G = graph(s,t);
h = plot(G,'Layout','force')

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

h = 
  GraphPlot with properties:

     NodeColor: [0 0.4470 0.7410]
    MarkerSize: 4
        Marker: 'o'
     EdgeColor: [0 0.4470 0.7410]
     LineWidth: 0.5000
     LineStyle: '-'
     NodeLabel: {'1'  '2'  '3'  '4'  '5'  '6'}
     EdgeLabel: {}
         XData: [-9.4159e-06 -1.1989 1.7509 -1.6352 0.8926 0.1906]
         YData: [2.0343e-05 -1.3279 0.3672 0.7324 -1.5533 1.7816]
         ZData: [0 0 0 0 0 0]

  Use GET to show all properties

通过增加节点 1 和 3 的标记大小突出显示这些节点。

highlight(h,[1 3])

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

通过更改节点 1 和 3 的颜色突出显示这些节点。

highlight(h,[1 3],'NodeColor','g')

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

创建并绘制一个图。返回 GraphPlot 对象的句柄 h

s = [1 1 1 1 1 1 2 3 4 5 6 7 7 7 7 8 9 10 11 8 6];
t = [2 3 4 5 6 7 3 4 5 6 2 8 9 10 11 10 10 11 8 1 11];
G = graph(s,t);
h = plot(G)

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

h = 
  GraphPlot with properties:

     NodeColor: [0 0.4470 0.7410]
    MarkerSize: 4
        Marker: 'o'
     EdgeColor: [0 0.4470 0.7410]
     LineWidth: 0.5000
     LineStyle: '-'
     NodeLabel: {'1'  '2'  '3'  '4'  '5'  '6'  '7'  '8'  '9'  '10'  '11'}
     EdgeLabel: {}
         XData: [-0.2157 -0.5149 -0.3637 -1.1504 -1.4097 -0.7742 0.9685 0.4346 1.9173 1.0259 0.0823]
         YData: [-0.4806 -1.0307 -1.7531 -1.4460 -0.5843 0.0987 0.6570 0.5422 1.4401 1.4411 1.1157]
         ZData: [0 0 0 0 0 0 0 0 0 0 0]

  Use GET to show all properties

计算图的最小生成树。通过增加绘图中最小生成树的边的线宽和更改其颜色,突出显示最小生成树子图。

[T,p] = minspantree(G);
highlight(h,T,'EdgeColor','r','LineWidth',1.5)

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

创建并绘制一个图。返回 GraphPlot 对象的句柄 h

n = 10;
A = delsq(numgrid('L',n+2));
G = graph(A,'omitselfloops'); 
G.Edges.Weight = ones(numedges(G),1);
h = plot(G);

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

通过将沿节点 74 和 21 之间最短路径的节点和边的颜色更改为绿色,突出显示该最短路径。

path = shortestpath(G,74,21);
highlight(h,path,'NodeColor','g','EdgeColor','g')

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

创建一个图,用于表示一个每侧各有 8 个节点的方形网格。绘制图并返回 GraphPlot 对象的句柄 p

n = 8;
A = delsq(numgrid('S',n+2));
G = graph(A,'omitselfloops');
p = plot(G);

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

查找节点 36 的相邻节点。

n36 = neighbors(G,36)
n36 = 4×1

    28
    35
    37
    44

使用 highlight 将节点 36 的颜色更改为绿色,其相邻节点和连接边的颜色更改为红色。

highlight(p,36,'NodeColor',[0 0.75 0])
highlight(p,n36,'NodeColor','red')
highlight(p,36,n36,'EdgeColor','red')

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

创建并绘制一个有向图。返回 GraphPlot 对象的句柄 h

G = digraph(bucky);
h = plot(G);

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

计算节点 1 和 56 之间的最大流。指定 maxflow 的两个输出来返回非零流的有向图 GF

[mf,GF] = maxflow(G,1,56)
mf = 3
GF = 
  digraph with properties:

    Edges: [28x2 table]
    Nodes: [60x0 table]

使用 highlight 更改包含非零流值的边的颜色。此外,将源节点 1 和目标节点 56 的颜色更改为绿色。

highlight(h,GF,'EdgeColor',[0.9 0.3 0.1],'NodeColor',[0.9 0.3 0.1])
highlight(h,[1 56],'NodeColor','g')

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

绘制多重图中两个节点之间的最短路径,并突出显示经过的特定边。

创建一个具有五个节点的加权多重图。有几对节点之间的边数超过一条。绘制图作为参考。

G = graph([1 1 1 1 1 2 2 3 3 3 4 4],[2 2 2 2 2 3 4 4 5 5 5 2],[2 4 6 8 10 5 3 1 5 6 8 9]);
p = plot(G,'EdgeLabel',G.Edges.Weight);

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

找出节点 1 和节点 5 之间的最短路径。由于有几个节点对之间的边数超过一条,因此为 shortestpath 指定三个输出,以返回最短路径所经过的特定边。

[P,d,edgepath] = shortestpath(G,1,5)
P = 1×5

     1     2     4     3     5

d = 11
edgepath = 1×4

     1     7     9    10

结果表明最短路径的总长度为 11,并沿 G.Edges(edgepath,:) 指定的边前进。

G.Edges(edgepath,:)
ans=4×2 table
    EndNodes    Weight
    ________    ______

     1    2       2   
     2    4       3   
     3    4       1   
     3    5       5   

通过使用 highlight 函数和 'Edges' 名称-值对组指定经过的各条边的索引来突出显示这条边路径。

highlight(p,'Edges',edgepath)

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

输入参数

全部折叠

输入图论图,指定为 GraphPlot 对象。使用 graphdigraph 函数创建一个图,然后结合使用 plot 与输出参数返回 GraphPlot 对象。

示例: H = plot(G)

要突出显示的节点,指定为逻辑向量,或指定为一个或多个节点索引或节点名称。如果 nodeIDs 是逻辑向量,则其长度必须为 numnodes(G)

下表显示通过数值节点索引或节点名称引用一个或多个节点的不同方法。

形式单一节点多个节点
节点索引

标量

示例:1

向量

示例:[1 2 3]

节点名称

字符向量

示例:'A'

字符向量元胞数组

示例:{'A' 'B' 'C'}

字符串标量

示例:"A"

字符串数组

示例:["A" "B" "C"]

nodeIDs 不得指定与 highlight 的任何可选参数名称冲突的节点名称,例如 'Edges''EdgeColor'。对于这些情况,请改用 findnode 传入节点索引。

要突出显示的图,指定为 graphdigraph 对象。G 必须具有与基础图 H 相同的节点,但具有的边是后者的边的子集。度为 0 的孤立节点不会突出显示。

节点对组,指定为单独的节点索引或节点名称参数。st 中位置类似的元素指定图中边的源和目标节点。

st 不得指定与 highlight 的任何可选参数名称冲突的节点名称,例如 'Edges''EdgeColor'。对于这些情况,请改用 findnode 传入节点索引。

示例: highlight(H,[1 2],[3 3]) 突出显示图边 (1,3)(2,3)

示例: highlight(H,'a','b') 突出显示从 'a''b' 的所有边。

名称-值参数

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

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

示例: highlight(H,nodes,'NodeColor','y')

要突出显示的边

全部折叠

要突出显示的边,以逗号分隔的对组形式指定,该对组由 'Edges' 和一个标量边索引、边索引向量或逻辑向量组成。当相同的两个节点之间存在多条边时,可以使用此名称-值对组来突出显示节点之间的特定边。

此名称-值对组的值可以是来自 shortestpathshortestpathtree 的第三个输出,例如 [path,d,edgepath] = shortestpath(…)

示例: highlight(p,'Edges',edgepath)

边属性

全部折叠

边颜色,指定为逗号分隔的对组,其中包含 'EdgeColor' 以及一个 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

    以下是 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

示例: plot(G,'EdgeColor','r') 创建一个具有红色边的图论图。

线型,指定为逗号分隔的对组,其中包含 'LineStyle' 和下表中列出的线型之一。

字符线型表示的线条
'-'实线

Sample of a solid line

'--'虚线

Sample of a dashed line

':'点线

Sample of a dotted line

'-.'点划线

Sample of a dash-dotted line

'none'无线条无线条

边线宽,指定为逗号分隔的对组,其中包含 'LineWidth' 和一个正值(以磅为单位)。

示例: 0.75

箭头大小,指定为以磅为单位的正值。对于具有 100 个或更少节点的图,ArrowSize 的默认值是 7,对于超过 100 个节点的图,默认值是 4

ArrowSize 仅用于有向图。

示例: 15

箭头在边上的位置,指定为 [0 1] 范围内的值。接近 0 的值表示箭头更靠近源节点,接近 1 的值表示箭头更靠近目标节点。默认值为 0.5,表示箭头位于源节点和目标节点的中间。

ArrowPosition 仅用于有向图。

节点属性

全部折叠

节点颜色,指定为逗号分隔的对组,其中包含 'NodeColor' 以及一个 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

    以下是 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

示例: plot(G,'NodeColor','k') 创建一个具有黑色节点的图论图。

节点标记符号,指定为逗号分隔的对组,其中包含 'Marker' 和下表中列出的字符向量之一。默认为对图节点使用圆形标记。

标记描述生成的标记
"o"圆圈

Sample of circle marker

"+"加号

Sample of plus sign marker

"*"星号

Sample of asterisk marker

"."

Sample of point marker

"x"叉号

Sample of cross marker

"_"水平线条

Sample of horizontal line marker

"|"垂直线条

Sample of vertical line marker

"square"方形

Sample of square marker

"diamond"菱形

Sample of diamond marker

"^"上三角

Sample of upward-pointing triangle marker

"v"下三角

Sample of downward-pointing triangle marker

">"右三角

Sample of right-pointing triangle marker

"<"左三角

Sample of left-pointing triangle marker

"pentagram"五角形

Sample of pentagram marker

"hexagram"六角形

Sample of hexagram marker

"none"无标记不适用

示例: '+'

示例: 'diamond'

节点标记大小,指定为逗号分隔的对组,其中包含 'MarkerSize' 和一个正值(以磅为单位)。对于具有 100 个或更少节点的图,默认标记大小是 4,对于超过 100 个节点的图,默认标记大小是 2

示例: 10

节点和边标签

全部折叠

节点标签颜色,指定为 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

以下是 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

示例: plot(G,'NodeLabel',C,'NodeLabelColor','m') 创建一个具有品红色节点标签的图论图。

边标签颜色,指定为 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

以下是 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

示例: plot(G,'EdgeLabel',C,'EdgeLabelColor','m') 创建一个具有品红色边标签的图论图。

字体

全部折叠

节点标签的字体名称,指定为支持的字体名称或 'FixedWidth'。要正确显示和打印,您必须选择系统支持的字体。默认字体取决于具体操作系统和区域设置。例如,Windows® 和 Linux® 英文系统默认使用 Helvetica 字体。

要使用在任何区域设置中都有较好显示效果的等宽字体,请指定 'FixedWidth'

示例: 'Cambria'

节点标签的字体大小,指定为正数。

节点标签中文本的粗细,指定为 'normal''bold'

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

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

并非所有字体都有加粗字体。

数据类型: cell | char | string

节点标签中文本的字符倾斜,指定为 'normal''italic'

  • 'italic' - 字符倾斜

  • 'normal' - 字符不倾斜

并非所有字体都有两种字体样式。

数据类型: cell | char | string

边标签的字体名称,指定为支持的字体名称或 'FixedWidth'。要正确显示和打印,您必须选择系统支持的字体。默认字体取决于具体操作系统和区域设置。例如,Windows 和 Linux 英文系统默认使用 Helvetica 字体。

要使用在任何区域设置中都有较好显示效果的等宽字体,请指定 'FixedWidth'

示例: 'Cambria'

边标签的字体大小,指定为正数。

边标签中文本的粗细,指定为 'normal''bold'

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

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

并非所有字体都有加粗字体。

数据类型: cell | char | string

边标签中文本的字符倾斜,指定为 'normal''italic'

  • 'italic' - 字符倾斜

  • 'normal' - 字符不倾斜

并非所有字体都有两种字体样式。

数据类型: cell | char | string

版本历史记录

在 R2015b 中推出