Main Content

highlight

Highlight nodes and edges in plotted graph

Description

example

highlight(H,nodeIDs) highlights the nodes specified by nodeIDs by increasing the sizes of their markers.

example

highlight(H,G) highlights the nodes and edges of graph G by increasing their node marker size and edge line width, respectively. G must have the same nodes and a subset of the edges of the underlying graph of H. If G contains repeated edges, then they are all highlighted. Isolated nodes with degree 0 are not highlighted.

highlight(H,s,t) highlights all edges between the specified source and target node pairs in s and t by increasing their edge line widths. If the edge indices are available instead of the node pairs (s,t), use highlight(H,'Edges',idx) instead.

example

highlight(___,Name,Value) uses additional options specified by one or more Name-Value pair arguments using any of the input argument combinations in previous syntaxes. For example, highlight(H,nodes,'NodeColor','g') highlights a subset of nodes by changing their color to green, instead of increasing their marker size.

Examples

collapse all

Create and plot a graph. Return a handle to the GraphPlot object, h.

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

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: [-1.7216e-04 -1.0659 1.7077 1.0376 -1.6982 0.0190]
         YData: [3.6622e-05 -1.4361 0.5342 -1.4577 0.5713 1.7884]
         ZData: [0 0 0 0 0 0]

  Use GET to show all properties

Highlight nodes 1 and 3 by increasing their marker size.

highlight(h,[1 3])

Highlight nodes 1 and 3 by changing their color.

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

Create and plot a graph. Return a handle to the GraphPlot object, 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)

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

Calculate the minimum spanning tree of the graph. Highlight the minimum spanning tree subgraph in the plot by increasing the line width and changing the color of the edges in the tree.

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

Create and plot a graph. Return a handle to the GraphPlot object, h.

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

Highlight the shortest path between nodes 74 and 21 by changing the color of the nodes and edges along the path to green.

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

Create a graph representing a square grid with a side of 8 nodes. Plot the graph and return a handle to the GraphPlot object, p.

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

Find the neighbors of node 36.

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

    28
    35
    37
    44

Use highlight to change the color of node 36 to green, and the color of its neighbors and their connecting edges to red.

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

Create and plot a directed graph. Return a handle to the GraphPlot object, h.

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

Compute the maximum flow between nodes 1 and 56. Specify two outputs to maxflow to return a directed graph of the nonzero flows, GF.

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

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

Use highlight to change the color of the edges that contain nonzero flow values. Also change the color of source node 1 and target node 56 to green.

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

Plot the shortest path between two nodes in a multigraph and highlight the specific edges that are traversed.

Create a weighted multigraph with five nodes. Several pairs of nodes have more than one edge between them. Plot the graph for reference.

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);

Find the shortest path between node 1 and node 5. Since several of the node pairs have more than one edge between them, specify three outputs to shortestpath to return the specific edges that the shortest path traverses.

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

     1     2     4     3     5

d = 11
edgepath = 1×4

     1     7     9    10

The results indicate that the shortest path has a total length of 11 and follows the edges given by 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 this edge path by using the highlight function with the 'Edges' name-value pair to specify the indices of the edges traversed.

highlight(p,'Edges',edgepath)

Input Arguments

collapse all

Input graph plot, specified as a GraphPlot object. Use the graph or digraph functions to create a graph, and then use plot with an output argument to return a GraphPlot object.

Example: H = plot(G)

Nodes to highlight, specified as a logical vector, or as one or more node indices or node names. If nodeIDs is a logical vector, then it must have length numnodes(G).

This table shows the different ways to refer to one or more nodes either by their numeric node indices or by their node names.

FormSingle NodeMultiple Nodes
Node index

Scalar

Example: 1

Vector

Example: [1 2 3]

Node name

Character vector

Example: 'A'

Cell array of character vectors

Example: {'A' 'B' 'C'}

String scalar

Example: "A"

String array

Example: ["A" "B" "C"]

nodeIDs must not specify node names that conflict with any of the optional parameter names for highlight, such as 'Edges' or 'EdgeColor'. Use findnode to instead pass in the node index for these cases.

Graph to highlight, specified as a graph or digraph object. G must have the same nodes and a subset of the edges of the underlying graph of H. Isolated nodes with degree 0 are not highlighted.

Node pairs, specified as separate arguments of node indices or node names. Similarly located elements in s and t specify the source and target nodes for edges in the graph.

s and t must not specify node names that conflict with any of the optional parameter names for highlight, such as 'Edges' or 'EdgeColor'. Use findnode to instead pass in the node index for these cases.

Example: highlight(H,[1 2],[3 3]) highlights the graph edges (1,3) and (2,3).

Example: highlight(H,'a','b') highlights all edges from 'a' to 'b'.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: highlight(H,nodes,'NodeColor','y')

Edges to Highlight

collapse all

Edges to highlight, specified as the comma-separated pair consisting of 'Edges' and a scalar edge index, vector of edge indices, or logical vector. Use this name-value pair to highlight a specific edge between nodes when multiple edges exist between the same two nodes.

The value of this name-value pair can be the third output from shortestpath or shortestpathtree, such as [path,d,edgepath] = shortestpath(…).

Example: highlight(p,'Edges',edgepath)

Edge Properties

collapse all

Edge color, specified as the comma-separated pair consisting of 'EdgeColor' and an RGB triplet, hexadecimal color code, or color name.

  • RGB triplets and hexadecimal color codes are useful for specifying custom colors.

    • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

    • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

    Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

    Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
    "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

    Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.

    RGB TripletHexadecimal Color CodeAppearance
    [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

Example: plot(G,'EdgeColor','r') creates a graph plot with red edges.

Line style, specified as the comma-separated pair consisting of 'LineStyle' and one of the line styles listed in this table.

Character(s)Line StyleResulting Line
'-'Solid line

Sample of a solid line

'--'Dashed line

Sample of a dashed line

':'Dotted line

Sample of a dotted line

'-.'Dash-dotted line

Sample of a dash-dotted line

'none'No lineNo line

Edge line width, specified as the comma-separated pair consisting of 'LineWidth' and a positive value in point units.

Example: 0.75

Arrow size, specified as a positive value in point units. The default value of ArrowSize is 7 for graphs with 100 or fewer nodes, and 4 for graphs with more than 100 nodes.

ArrowSize is used only for directed graphs.

Example: 15

Position of arrow along edge, specified as a value in [0 1]. A value near 0 places arrows closer to the source node, and a value near 1 places arrows closer to the target node. The default value is 0.5 so that the arrows are halfway between the source and target nodes.

ArrowPosition is used only for directed graphs.

Node Properties

collapse all

Node color, specified as the comma-separated pair consisting of 'NodeColor' and an RGB triplet, hexadecimal color code, or color name.

  • RGB triplets and hexadecimal color codes are useful for specifying custom colors.

    • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

    • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

    Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

    Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
    "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

    Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

    RGB TripletHexadecimal Color CodeAppearance
    [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

Example: plot(G,'NodeColor','k') creates a graph plot with black nodes.

Node marker symbol, specified as the comma-separated pair consisting of 'Marker' and one of the character vectors listed in this table. The default is to use circular markers for the graph nodes.

MarkerDescriptionResulting Marker
"o"Circle

Sample of circle marker

"+"Plus sign

Sample of plus sign marker

"*"Asterisk

Sample of asterisk marker

"."Point

Sample of point marker

"x"Cross

Sample of cross marker

"_"Horizontal line

Sample of horizontal line marker

"|"Vertical line

Sample of vertical line marker

"square"Square

Sample of square marker

"diamond"Diamond

Sample of diamond marker

"^"Upward-pointing triangle

Sample of upward-pointing triangle marker

"v"Downward-pointing triangle

Sample of downward-pointing triangle marker

">"Right-pointing triangle

Sample of right-pointing triangle marker

"<"Left-pointing triangle

Sample of left-pointing triangle marker

"pentagram"Pentagram

Sample of pentagram marker

"hexagram"Hexagram

Sample of hexagram marker

"none"No markersNot applicable

Example: '+'

Example: 'diamond'

Node marker size, specified as the comma-separated pair consisting of 'MarkerSize' and a positive value in point units. The default marker size is 4 for graphs with 100 or fewer nodes, and 2 for graphs with more than 100 nodes.

Example: 10

Node and Edge Labels

collapse all

Node label color, specified as an RGB triplet, hexadecimal color code, or color name.

RGB triplets and hexadecimal color codes are useful for specifying custom colors.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"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

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[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

Example: plot(G,'NodeLabel',C,'NodeLabelColor','m') creates a graph plot with magenta node labels.

Edge label color, specified as an RGB triplet, hexadecimal color code, or color name.

RGB triplets and hexadecimal color codes are useful for specifying custom colors.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"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

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[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

Example: plot(G,'EdgeLabel',C,'EdgeLabelColor','m') creates a graph plot with magenta edge labels.

Font

collapse all

Font name for node labels, specified as a supported font name or 'FixedWidth'. To display and print properly, you must choose a font that your system supports. The default font depends on the specific operating system and locale. For example, Windows® and Linux® systems in English localization use the Helvetica font by default.

To use a fixed-width font that looks good in any locale, specify 'FixedWidth'.

Example: 'Cambria'

Font size for node labels, specified as a positive number.

Thickness of text in node labels, specified as 'normal' or 'bold':

  • 'bold'Thicker character outlines than normal

  • 'normal' — Normal weight as defined by the particular font

Not all fonts have a bold font weight.

Data Types: cell | char | string

Character slant of text in node labels, specified as 'normal' or 'italic':

  • 'italic'Slanted characters

  • 'normal' — No character slant

Not all fonts have both font styles.

Data Types: cell | char | string

Font name for edge labels, specified as a supported font name or 'FixedWidth'. To display and print properly, you must choose a font that your system supports. The default font depends on the specific operating system and locale. For example, Windows and Linux systems in English localization use the Helvetica font by default.

To use a fixed-width font that looks good in any locale, specify 'FixedWidth'.

Example: 'Cambria'

Font size for edge labels, specified as a positive number.

Thickness of text in edge labels, specified as 'normal' or 'bold':

  • 'bold'Thicker character outlines than normal

  • 'normal' — Normal weight as defined by the particular font

Not all fonts have a bold font weight.

Data Types: cell | char | string

Character slant of text in edge labels, specified as 'normal' or 'italic':

  • 'italic'Slanted characters

  • 'normal' — No character slant

Not all fonts have both font styles.

Data Types: cell | char | string

Version History

Introduced in R2015b