Directed Graph Layouts

75 views
Skip to first unread message

Jennifer DiCarlo

unread,
Mar 9, 2024, 3:21:31 AMMar 9
to networkx-discuss
Hello, all.  I've been playing around with using networkx to create a graph layout which I can then send to tikz...

Do Directed Graphs treat layouts differently than Undirected Graphs?   

The following screenshots show the tikzpictures created from the Undirected Graph (top photo) and the Directed Graph (bottom photo).  The ONLY thing I changed in my code was the method of creating the graphs (see the line of code before each screenshot).

Why would the layouts come out so differently?  I am using the same node/edge dataframes....

I am using the Kamada_Kawai layout, none of the other layouts produced a useful result for me...at least when I was only working with the Undirected version.....

# Undirected
G = nx.Graph(list(all_edge_df.index.values))

Screenshot 2024-03-08 at 20.31.11.png
# Directed
G = nx.DiGraph(list(all_edge_df.index.values))
Screenshot 2024-03-08 at 20.33.15.png

**Note that the reason I experimented with Directed Graphs is a topic for another thread...

Dan Schult

unread,
Mar 9, 2024, 12:59:48 PMMar 9
to networkx...@googlegroups.com
Many of the layout routines start with a random set of points, and then iteratively adjust the layout. So even for two calls for the same graph you can get vastly different layouts. It doesn't have anything to do with them being directed or undirected. You can use the input arguments to specify the starting layout (for many of the layout functions). And you can initialize the random number generator using the `seed` input arg if you prefer to start randomly but let the computer pick that random output.

If your nodes are the same, I would suggest saving the layout for the undirected case and using that for all plots that have those same nodes (directed or undirected).  Another approach (which works if many but not all the nodes are the same) is to save the positions from the first layout and then add any new nodes along with manually given starting positions to construct the initial positions for the next layout you want to make. Having extra nodes in the `pos` dict shouldn't affect the layouts. But there is no guarantee that this will look similar to the first layout. They can change a fair amount due to the nature of the algorithm. YMMV...
:)

Jennifer DiCarlo

unread,
Mar 11, 2024, 9:44:09 PMMar 11
to networkx-discuss
If it was just the random seed wouldn't the layout be different every time?  I have created the undirected graph dozens of times, and tried the directed one several times (including once just to get the screenshots for this post).

I can try using the position values from the undirected graph.  I was just surprised the two came out so differently...same nodes and edges after all.

Reply all
Reply to author
Forward
0 new messages