Improving Python NetworkX graph layout

Sayan picture Sayan · Feb 24, 2014 · Viewed 43k times · Source

I am having some problems in visualizing the graphs created with python-networkx, I want to able to reduce clutter and regulate the distance between the nodes (I have also tried spring_layout, it just lays out the nodes in an elliptical fashion). Please advise. enter image description here

Parts of code:

nx.draw_networkx_edges(G, pos, edgelist=predges, edge_color='red', arrows=True)
nx.draw_networkx_edges(G, pos, edgelist=black_edges, arrows=False, style='dashed')
# label fonts
nx.draw_networkx_labels(G,pos,font_size=7,font_family='sans-serif')
nx.draw_networkx_edge_labels(G,pos,q_list,label_pos=0.3)

Answer

Hooked picture Hooked · Feb 24, 2014

In networkx, it's worth checking out the graph drawing algorithms provided by graphviz via nx.graphviz_layout.

I've had good success with neato but the other possible inputs are

  • dot - "hierarchical" or layered drawings of directed graphs. This is the default tool to use if edges have directionality.

  • neato - "spring model'' layouts. This is the default tool to use if the graph is not too large (about 100 nodes) and you don't know anything else about it. Neato attempts to minimize a global energy function, which is equivalent to statistical multi-dimensional scaling.

  • fdp - "spring model'' layouts similar to those of neato, but does this by reducing forces rather than working with energy.

  • sfdp - multiscale version of fdp for the layout of large graphs.

  • twopi - radial layouts, after Graham Wills 97. Nodes are placed on concentric circles depending their distance from a given root node.

  • circo - circular layout, after Six and Tollis 99, Kauffman and Wiese 02. This is suitable for certain diagrams of multiple cyclic structures, such as certain telecommunications networks.

In general, graph drawing is a hard problem. If these algorithms are not sufficient, you'll have to write your own or have networkx draw parts individually.