Plotting an undirected graph gives me only unconnected dots.

I have an undirected graph of 7618 edges that I want to plot. I put the first set of nodes of each edge in nodes1 and the other set of nodes of each edge in nodes2 and:
G= graph(nodes1,nodes2)
plot(G)
This is the plot I get
untitled.png
Why aren't the nodes connected like they are supposed to except from those in the bottom left? How do I fix it to get the results I want?
Matlab R2016a

 Accepted Answer

I found a solution for deleting the isolated nodes. First you find the degree matrix for every node. The elements which have a degree of zero are isolated nodes. Therefore your next move is to find the indexes of those elements and the delete them from the graph:
D = degree(G);
index = find(D == 0);
D(index) = [];
G = rmnode(G,index);

More Answers (1)

It's going to be impossible to give a specific answer without seeing your nodes vectors (or a small sample of the nodes vectors.)
But generally, this indicates most of your nodes aren't connected to any other edges. If I had to guess I'd say that you have one element in either nodes1 or nodes2 that is larger than the others, meaning MATLAB will add isolated nodes up to that maximum value. Consider:
G = graph(7, 8);
plot(G)
You may expect G has exactly two nodes, labeled 7 and 8. It actually has eight where nodes 1 through 6 aren't connected to anything else.

2 Comments

Thanks a lot for the answer, it makes a lot of sense why my graph looks how it looks now. But how do I not graph those isolated nodes?
I found a solution for deleting the isolated nodes. First you find the degree matrix for every node. The elements which have a degree of zero are isolated nodes. Therefore your next move is to find the indexes of those elements and the delete them from the graph:
D = degree(G);
index = find(D == 0);
D(index) = [];
G = rmnode(G,index);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!