layoutcoords
Syntax
Description
calculates the coordinates of the nodes of graph nodeCoords
= layoutcoords(G
)G
using an automatic
choice of layout method based on the structure of the graph.
[
also calculates the coordinates of the graph edges.nodeCoords
,edgeCoords
] = layoutcoords(G
)
[___] = layoutcoords(
specifies additional options using one or more name-value arguments. Which name-value
arguments are supported depends on the choice of layout method. For example,
G
,method
,Name=Value)layoutcoords(G,"force",Iterations=N)
specifies the number of iterations
to use in computing the force layout, and
layoutcoords(G,"layered",Sources=S)
uses a layered layout with source
nodes S
included in the first layer.
Examples
Automatic Layout Method
Create a graph with eight nodes and twelve edges.
s = [1 1 1 2 2 3 3 4 5 5 6 7]; t = [2 4 5 3 6 4 7 8 6 8 7 8]; G = graph(s,t)
G = graph with properties: Edges: [12x1 table] Nodes: [8x0 table]
Calculate the coordinates of the nodes and edges using an automatic choice of layout method.
[nodeCoords, edgeCoords] = layoutcoords(G)
nodeCoords = 8×2
-0.0495 -2.0789
-0.0119 -0.5215
-1.5285 0.8917
-1.5694 -0.8184
1.5285 -0.8917
1.5694 0.8184
0.0495 2.0789
0.0119 0.5215
edgeCoords=12×1 cell array
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
{2x2 double}
Layered Layout
Create a graph with 10 nodes and 12 edges.
s = [1 1 1 1 1 1 1 1 4 1 9 8]; t = [2 3 4 5 6 7 8 9 10 10 10 9]; g = graph(s,t)
g = graph with properties: Edges: [12x1 table] Nodes: [10x0 table]
Calculate the coordinates of the nodes and edges using a layered layout method. Specify Sources=1
to use the first node in the first layer.
[nodeCoords, edgeCoords] = layoutcoords(g,"layered",Sources=1)
nodeCoords = 10×2
5.0000 5.0000
1.0000 4.0000
2.0000 4.0000
4.0000 4.0000
7.0000 4.0000
8.0000 4.0000
9.0000 4.0000
4.5000 1.0000
5.0000 2.0000
4.5000 3.0000
edgeCoords=12×1 cell array
{ 2x2 double}
{ 2x2 double}
{ 2x2 double}
{ 2x2 double}
{ 2x2 double}
{ 2x2 double}
{41x2 double}
{31x2 double}
{21x2 double}
{ 2x2 double}
{ 2x2 double}
{ 2x2 double}
Plot the graph nodes and edges using the coordinates in nodeCoords
and edgeCoords
. Use a for
-loop to plot the entries in edgeCoords
one at a time.
xn = nodeCoords(:,1); yn = nodeCoords(:,2); plot(xn,yn,"bo","MarkerFaceColor","b") hold on for k = 1:numedges(g) e = edgeCoords{k}; plot(e(:,1),e(:,2),"r-") end hold off
Input Arguments
method
— Layout method
"auto"
(default) | "circle"
| "force"
| "layered"
| "subspace"
| "force3"
| "subspace3"
Layout method, specified as one of the options in the table. The table also lists compatible name-value pairs to further refine each layout method.
Option | Description | Layout-Specific Name-Value Pairs |
---|---|---|
"auto" (default) | Automatic choice of layout method based on the size and structure of the graph. | — |
"circle" | Circular layout. Places the graph nodes on a circle centered at the origin with radius 1. |
|
"force" | Force-directed layout [1]. Uses attractive forces between adjacent nodes and repulsive forces between distant nodes. |
|
"layered" | Layered layout [2], [3], [4]. Places the graph nodes into a set of layers, revealing hierarchical structure. By default the layers progress downwards (the arrows of a directed acyclic graph point down). |
|
"subspace" | Subspace embedding layout [5]. Plots the graph nodes in a high-dimensional embedded subspace, and then projects the positions back into 2-D. By default the subspace dimension is either 100 or the total number of nodes, whichever is smaller. |
|
"force3" | 3-D force-directed layout. |
|
"subspace3" | 3-D subspace embedding layout. |
|
Example: layoutcoords(G,"layered")
Example: layoutcoords(G,"force3","Iterations",10)
Example: layoutcoords(G,"subspace","Dimension",50)
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.
Example: layoutcoords(G,"subspace","Dimension",200)
Iterations
— Number of force-directed layout iterations
100
(default) | positive scalar integer
Number of force-directed layout iterations, specified as a positive scalar integer.
This option is available only when method
is
"force"
or "force3"
.
Example: layoutcoords(G,"force",Iterations=250)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
WeightEffect
— Effect of edge weights on layout
"none"
(default) | "direct"
| "inverse"
Effect of edge weights on layout, specified as one of the values in this table. If
there are multiple edges between two nodes (as in a directed graph with an edge in
each direction, or a multigraph), then the weights are summed before computing
WeightEffect
.
This option is available only when method
is
"force"
or "force3"
.
Value | Description |
---|---|
| Edge weights do not affect the layout. |
| Edge length is proportional to the edge weight,
|
| Edge length is inversely proportional to the edge weight,
|
Example: layoutcoords(G,"force",WeightEffect="inverse")
UseGravity
— Gravity toggle for layouts with multiple components
"off"
or false
(default) | "on"
or true
Gravity toggle for layouts with multiple components, specified as either
"off"
, "on"
, true
, or
false
. A value of "on"
is equivalent to
true
, and "off"
is equivalent to
false
.
By default, MATLAB® lays out graphs with multiple components on a grid. The grid can obscure
the details of larger components since they are given the same amount of space as
smaller components. With UseGravity
set to "on"
or true
, multiple components are instead laid out radially around
the origin. This layout spreads out the components in a more natural way, and provides
more space for larger components.
This option is available only when method
is
"force"
or "force3"
.
Example: layoutcoords(G,"force",UseGravity=true)
Data Types: char
| logical
XStart
— Starting x-coordinates for nodes
vector
Starting x-coordinates for nodes, specified as a vector of node coordinates. Use
this option together with YStart
to specify 2-D starting
coordinates (or with YStart
and ZStart
to
specify 3-D starting coordinates) before iterations of the force-directed algorithm
change the node positions.
This option is available only when method
is
"force"
or "force3"
.
Example: layoutcoords(G,"force",XStart=x,YStart=y)
Example: layoutcoords(G,"force3",XStart=x,YStart=y,ZStart=z)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
YStart
— Starting y-coordinates for nodes
vector
Starting y-coordinates for nodes, specified as a vector of node coordinates. Use
this option together with XStart
to specify 2-D starting
coordinates (or with XStart
and ZStart
to
specify 3-D starting coordinates) before iterations of the force-directed algorithm
change the node positions.
This option is available only when method
is
"force"
or "force3"
.
Example: layoutcoords(G,"force",XStart=x,YStart=y)
Example: layoutcoords(G,"force3",XStart=x,YStart=y,ZStart=z)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
ZStart
— Starting z-coordinates for nodes
vector
Starting z-coordinates for nodes, specified as a vector of node coordinates. Use
this option together with XStart
and YStart
to
specify the starting x, y, and
z node coordinates before iterations of the force-directed
algorithm change the node positions.
This option is available only when method
is
"force3"
.
Example: layoutcoords(G,"force3",XStart=x,YStart=y,ZStart=z)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Direction
— Direction of layers
"down"
(default) | "up"
| "left"
| "right"
Direction of layers, specified as either "down"
,
"up"
, "left"
or "right"
.
For directed acyclic (DAG) graphs, the arrows point in the indicated direction.
This option is available only when method
is
"layered"
.
Example: layoutcoords(G,"layered",Direction="up")
Sources
— Nodes to include in the first layer
node indices | node names
Nodes to include in the first layer, specified as one or more node indices or node names.
This table shows the different ways to refer to one or more nodes either by their numeric node indices or by their node names.
Form | Single Node | Multiple Nodes |
---|---|---|
Node index | Scalar Example: | Vector Example: |
Node name | Character vector Example: | Cell array of character vectors Example: |
String scalar Example: | String array Example: |
This option is available only when method
is
"layered"
.
Example: layoutcoords(G,"layered",Sources=[1 3
5])
Sinks
— Nodes to include in the last layer
node indices | node names
Nodes to include in the last layer, specified as one or more node indices or node names.
This option is available only when method
is
"layered"
.
Example: layoutcoords(G,"layered",Sinks=[2 4 6])
AssignLayers
— Layer assignment method
"auto"
(default) | "asap"
| "alap"
Layer assignment method, specified as one of the options in this table.
Option | Description |
---|---|
"auto" (default) | Node assignment uses either "asap" or
"alap" , whichever is more compact. |
"asap" | As soon as possible. Each node is assigned to the first possible layer, given the constraint that all its predecessors must be in earlier layers. |
"alap" | As late as possible. Each node is assigned to the last possible layer, given the constraint that all its successors must be in later layers. |
This option is available only when method
is
"layered"
.
Example: layoutcoords(G,"layered",AssignLayers="alap")
Dimension
— Dimension of embedded subspace
positive scalar integer
Dimension of embedded subspace, specified as a positive scalar integer.
The default integer value is
min([100, numnodes(G)])
.For the
"subspace"
layout, the integer must be greater than or equal to 2.For the
"subspace3"
layout, the integer must be greater than or equal to 3.In both cases, the integer must be less than the number of nodes.
This option is available only when method
is
"subspace"
or "subspace3"
.
Example: layoutcoords(G,"subspace",Dimension=d)
Example: layoutcoords(G,"subspace3",Dimension=d)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Center
— Center node in circular layout
node index | node name
Center node in circular layout, specified as one of the values in this table.
Value | Example |
---|---|
Scalar node index | 1 |
Character vector node name | 'A' |
String scalar node name | "A" |
This option is available only when method
is
"circle"
.
Example: layoutcoords(G,"circle",Center=3)
places node three at
the center.
Example: layoutcoords(G,"circle",Center="Node1")
places the node
named "Node1"
at the center.
Output Arguments
nodeCoords
— Node layout coordinates
matrix
Node layout coordinates, returned as a matrix with a number of rows equal to
numnodes(G)
. For 2-D layouts the matrix has two columns, while for
3-D layouts the matrix has three columns. Each row of the matrix gives the
x-, y-, and optionally
z-coordinates of each node in the layout.
edgeCoords
— Edge layout coordinates
cell array
Edge layout coordinates, returned as a cell array of size
numedges(G)
-by-1
. Each element of the cell array
contains a matrix giving the x-, y-, and
optionally z-coordinates of each point on the edge. Each edge has at
least two points.
References
[1] Fruchterman, T., and E. Reingold,. “Graph Drawing by Force-directed Placement.” Software — Practice & Experience. Vol. 21 (11), 1991, pp. 1129–1164.
[2] Gansner, E., E. Koutsofios, S. North, and K.-P Vo. “A Technique for Drawing Directed Graphs.” IEEE Transactions on Software Engineering. Vol.19, 1993, pp. 214–230.
[3] Barth, W., M. Juenger, and P. Mutzel. “Simple and Efficient Bilayer Cross Counting.” Journal of Graph Algorithms and Applications. Vol.8 (2), 2004, pp. 179–194.
[4] Brandes, U., and B. Koepf. “Fast and Simple Horizontal Coordinate Assignment.” LNCS. Vol. 2265, 2002, pp. 31–44.
[5] Y. Koren. “Drawing Graphs by Eigenvectors: Theory and Practice.” Computers and Mathematics with Applications. Vol. 49, 2005, pp. 1867–1888.
Version History
Introduced in R2024b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)