Drawing a network of nodes in circular formation with links between nodes

Vass picture Vass · Apr 27, 2011 · Viewed 14.1k times · Source

I would like to draw a circular graph of nodes where certain nodes have a link between them. Here are a few examples from social network graphs:

example1
(source: wrightresult.com)

example2

example3
(source: twit88.com)

How can this be done with MATLAB? Is it possible without installing a separate package?

Answer

abcd picture abcd · Apr 27, 2011

Here is one way you can do what you want. First, generate points on the circle that you are interested in

clear;
theta=linspace(0,2*pi,31);theta=theta(1:end-1);
[x,y]=pol2cart(theta,1);

Next, if you know the pairs of nodes that are connected, you can skip this step. But in many cases, you get a connectivity matrix from other computations, and you find the indices of the connected nodes from that. Here, I've created a Boolean matrix of connections. So, if there are N nodes, the connectivity matrix is an NxN symmetric matrix, where if the i,jth element is 1, it means you have a connection from node i to node j and 0 otherwise. You can then extract the subscripts of the non-zero pairs to get node connections (only the upper triangle is needed).

links=triu(round(rand(length(theta))));%# this is a random list of connections
[ind1,ind2]=ind2sub(size(links),find(links(:)));

This is the connectivity matrix I generated with the code above.

enter image description here

Now we just need to plot the connections, one at a time

h=figure(1);clf(h);
plot(x,y,'.k','markersize',20);hold on
arrayfun(@(p,q)line([x(p),x(q)],[y(p),y(q)]),ind1,ind2);
axis equal off

which will give you a figure similar to your examples

enter image description here