How are coordinates in plot(graph(M)) generated?

3 views (last 30 days)
Given an undirected unweighted adjacency matrix M consisting of only ones and zeros, Matlab can generate a visual graph representation using:
p = plot(graph(M))
Depending on what the exact configuration of M is, the coordinates of p will be different. I have two questions:
  1. What is the default method used by Matlab for generating coordinates in the plot p?
  2. Are there alternatives to the default method, for example, if one would like to base location of nodes in the plot on some centrality measure of choice?

Accepted Answer

Christine Tobler
Christine Tobler on 21 Dec 2015
There are different layout methods for plotting a graph, which you can call as follows:
plot(graph(M), 'layout', LAY);
with LAY one of 'force', 'subspace', 'layered' and 'circle'. By default, one of the first three is chosen based on the structure and size of the input graph.
Alternative, you can compute the x- and y-coordinates of each node yourself, and use the command
plot(graph(M), 'XData', x, 'YData', y);
I'm not aware of methods to plot a graph based on a centrality measure. Do you have more information about this?
  3 Comments
Christine Tobler
Christine Tobler on 22 Dec 2015
Currently, the choice between layouts is:
  • 'subspace' for graphs with more than 100 nodes
  • 'layered' for graphs with <= 100 nodes that have no cycles
  • 'force' for graphs with <= 100 nodes that have cycles
For the second point, MATLAB does not provide such a layout method, but here is some code that generates something similar:
% Generate graph with few very central nodes:
M = sprandn(80, 80, 0.01);
M(1:5, :) = sprandn(5, 80, 0.3);
g = graph(M, 'upper');
h = plot(g);
% Layout plot based on degree-centrality:
d = degree(g);
r = max(d) + 2 - d;
phi = linspace(0, 360, numnodes(g)+1)';
phis = phi;
phis(end) = [];
h.XData = r.*cosd(phis);
h.YData = r.*sind(phis);
for ii=2:max(d)+2
if any(r == ii)
hold on;
plot(ii*cosd(phi), ii*sind(phi), 'Color', [0.8 0.8 0.8]);
end
end
uistack(h, 'top')

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!