Posts

Showing posts with the label Networkx

Can One Get Hierarchical Graphs From Networkx With Python 3?

Image
Answer : [scroll down a bit to see what kind of output the code produces] edit (7 Nov 2019) I've put a more refined version of this into a package I've been writing: https://epidemicsonnetworks.readthedocs.io/en/latest/_modules/EoN/auxiliary.html#hierarchy_pos. The main difference between the code here and the version there is that the code here gives all children of a given node the same horizontal space, while the code following that link also considers how many descendants a node has when deciding how much space to allocate it. edit (19 Jan 2019) I have updated the code to be more robust: It now works for directed and undirected graphs without any modification, no longer requires the user to specify the root, and it tests that the graph is a tree before it runs (without the test it would have infinite recursion - see user2479115's answer for a way to handle non-trees). edit (27 Aug 2018) If you want to create a plot with the nodes appearing as rings around th...

Construct NetworkX Graph From Pandas DataFrame

Answer : NetworkX expects a square matrix (of nodes and edges), perhaps* you want to pass it: In [11]: df2 = pd.concat([df, df.T]).fillna(0) Note: It's important that the index and columns are in the same order! In [12]: df2 = df2.reindex(df2.columns) In [13]: df2 Out[13]: Bar Bat Baz Foo Loc 1 Loc 2 Loc 3 Loc 4 Loc 5 Loc 6 Loc 7 Quux Bar 0 0 0 0 0 0 1 1 0 1 1 0 Bat 0 0 0 0 0 0 1 0 0 1 0 0 Baz 0 0 0 0 0 0 1 0 0 0 0 0 Foo 0 0 0 0 0 0 1 1 0 0 0 0 Loc 1 0 0 0 0 0 0 0 0 0 0 0 1 Loc 2 0 0 0 0 0 0 0 0 0 0 0 0 Loc 3 1 1 1 1 0 0 0 0 0 0 0 0 Loc 4 1 0 0 1 0 0 0 0 0 0 ...