gplot - Plot nodes and edges in adjacency matrix - MATLAB (original) (raw)

Plot nodes and edges in adjacency matrix

Syntax

Description

gplot([A](#mw%5F9d797f8a-80f5-4096-8fa4-1f97b2c36303),[XYCoords](#mw%5F5fc2f29c-2d8a-4278-ad8f-17132f3bf9a8)) plots a graph of the nodes and edges defined in the adjacency matrix A at the coordinates specified in XYCoords. The adjacency matrixA is an n-by-n matrix, wheren is the number of nodes. XYCoords is ann-by-2 matrix specifying_xy_-coordinates for each node.

example

gplot([A](#mw%5F9d797f8a-80f5-4096-8fa4-1f97b2c36303),[XYCoords](#mw%5F5fc2f29c-2d8a-4278-ad8f-17132f3bf9a8),[LineSpec](#f23-268532%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)) additionally uses LineSpec to specify the line type, marker symbol, and color to use in the plot. For example, gplot(A,XY,'r-*') uses red lines for edges and red asterisks for nodes.

example

[[x](#mw%5Fa66e1414-bd9e-4fba-8731-b444297bc5f8),[y](#mw%5Fa66e1414-bd9e-4fba-8731-b444297bc5f8)] = gplot([A](#mw%5F9d797f8a-80f5-4096-8fa4-1f97b2c36303),[XYCoords](#mw%5F5fc2f29c-2d8a-4278-ad8f-17132f3bf9a8)) returns the NaN-delimited vectors x andy without generating a plot. Use x andy to generate a plot at a later time usingplot(x,y).

Examples

collapse all

Plot Graph of Nodes Using Asterisks

Plot half of the carbon-60 molecule, placing asterisks at each node.

k = 1:30; [B,XY] = bucky; gplot(B(k,k),XY(k,[1 2]),'-*') axis square

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

Input Arguments

collapse all

A — Adjacency matrix

matrix

Adjacency matrix, specified as a matrix. A describes the connections between the nodes in the graph by the location of nonzero values. If nodei and node j are connected, thenA(i,j) or A(j,i) is nonzero; otherwise,A(i,j) and A(j,i) are zero.

Example: A = ones(5) is the adjacency matrix of a graph with five nodes where each node is connected to all the others.

Example: A = [0 1 1 1; 1 0 0 0; 1 0 0 0; 1 0 0 0] is the adjacency matrix of a graph with four nodes where one node connects to the other three.

Data Types: single | double

XYCoords — _xy_-coordinates of nodes

N-by-2 matrix

_xy_-coordinates of nodes, specified as anN-by-2 matrix. Each row inXYCoords defines the coordinates for one node in the graph, soXYCoords(i,:) = [x(i) y(i)] gives the coordinates for nodei.

Example: XYCoords = [1 2; 3 4] plots one node at (1,2) and a second node at (3,4).

LineSpec — Line style, marker, and color

string scalar | character vector

Line style, marker, and color, specified as a string scalar or character vector containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.

Example: "--or" is a red dashed line with circle markers.

Line Style Description Resulting Line
"-" Solid line Sample of solid line
"--" Dashed line Sample of dashed line
":" Dotted line Sample of dotted line
"-." Dash-dotted line Sample of dash-dotted line, with alternating dashes and dots
Marker Description Resulting 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
Color Name Short Name RGB Triplet Appearance
"red" "r" [1 0 0] Sample of the color red
"green" "g" [0 1 0] Sample of the color green
"blue" "b" [0 0 1] Sample of the color blue
"cyan" "c" [0 1 1] Sample of the color cyan
"magenta" "m" [1 0 1] Sample of the color magenta
"yellow" "y" [1 1 0] Sample of the color yellow
"black" "k" [0 0 0] Sample of the color black
"white" "w" [1 1 1] Sample of the color white

Output Arguments

collapse all

x, y — Node coordinates

vectors

Node coordinates, returned as vectors. x and y contain the same information as XYCoords, but in a different format that is suitable for plotting with the command plot(x,y). The line segments defined in x and y are separated withNaN values.

Tips

Version History

Introduced before R2006a