MATLAB Answers

John Doe

How to graph a connectivity/adjacency matrix?

Asked by John Doe
on 26 Apr 2013

I want to graph the structure of a network (a power grid). I have a list containing to-from nodes for each branch.

The BIOGRAPH function is almost perfect. The drawback is that the lines always go out the "bottom" of the ancestor block, and into the "top" of the descendant. As an ancestor is always displayed above its descendants, the graphs are sometimes very chaotic.

It would be much better if the graph was plotted in a way that just showed which nodes were connected, without any hierarchy, and where lines could be horizontal.

Edit: I need to be able to assign different colors to various lines / nodes, to visualize voltage issues or overloads etc, similar to what I've done using biograph (code below).

Is this possible? It doesn't need to be a perfect solution, any improvements would be great.

This is the code I use now:

%% Plot biograph
Sys = sparse(from,to,1,s,s);
SysTri = tril(Sys + Sys'); 
bg = biograph(SysTri,ids,'ShowArrows','off','ShowWeights','off');
h = view(bg);  
 %% Color faulted line:
set(h.nodes(newFaultNodes),'Color',[1 0.4 0.4]);
fowEdges = getedgesbynodeid(h,get(h.Nodes(newFaultNodes),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(newFaultNodes)),'ID'));
edges = [fowEdges;revEdges];
set(edges,'LineColor',[1 0 0])



No products are associated with this question.

1 Answer

Answer by Kelly Kearney
on 29 Apr 2013
Edited by Kelly Kearney
on 29 Apr 2013
 Accepted answer

Would gplot work for the basic setup? By default, the code doesn't allow the edge color modifications that you mention, but the code can be modified pretty easily to allow this (I've written a version that allows this, and can post it to the FEX if this is the type of thing you had in mind).


Hi Kelly, thanks for your help!

Graphviz looks very promising, but unfortunately I can't get it to work. I have installed the program, and I believe I have the files in the correct folders. When I run the demos, I get a plot, but I get error messages saying MATLAB can't find 'neato', or any of the other layout engines in the bin-folder.

The result is that the appropriate coordinates are not assigned, thus every node is placed either in a circle or randomly. (All other properties are assigned, like colors, arrows, shapes etc.)

I don't have time to work more on this problem now, but I will try graphviz again in a few days and let you know.

What operating system are you on, and how do you start Matlab? At least on a Mac, Matlab doesn't run any of your .rc or .profile scripts when it sets up the shell it accesses for system commands, so it can be a bit of a pain to get it to recognize some external functions.

The easiest way to resolve this is to run

 setenv('PATH' [getenv('PATH') ':/path/to/neato']);

(or whatever corresponds to your operating system).

I actually solve this problem by adding

 source ~/.bash_profile

to the first line of the {matlabroot}/bin/matlab function, because this way I don't need to worry about syncing my Matlab shell with the rest of my computer when I make changes to my path, but that's probably not the recommended way to do it (since the main matlab startup script isn't really intended to be messed with).

setenv worked =)

It looks like Graphviz has all the functions is need. Thank you, I really appreciate the help!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!