how to draw multigraph in networkx using matplotlib or graphviz

Graphviz does a good job drawing parallel edges. You can use that with NetworkX by writing a dot file and then processing with Graphviz (e.g. neato layout below). You’ll need pydot or pygraphviz in addition to NetworkX In [1]: import networkx as nx In [2]: G=nx.MultiGraph() In [3]: G.add_edge(1,2) In [4]: G.add_edge(1,2) In [5]: nx.write_dot(G,’multi.dot’) … Read more

Drawing multiple edges between two nodes with networkx

An improvement to the reply above is adding the connectionstyle to nx.draw, this allows to see two parallel lines in the plot: import networkx as nx import matplotlib.pyplot as plt G = nx.DiGraph() #or G = nx.MultiDiGraph() G.add_node(‘A’) G.add_node(‘B’) G.add_edge(‘A’, ‘B’, length = 2) G.add_edge(‘B’, ‘A’, length = 3) pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True, … Read more

networkx – change color/width according to edge attributes – inconsistent result

The order of the edges passed to the drawing functions are important. If you don’t specify (using the edges keyword) you’ll get the default order of G.edges(). It is safest to explicitly give the parameter like this: import networkx as nx G = nx.Graph() G.add_edge(1,2,color=”r”,weight=2) G.add_edge(2,3,color=”b”,weight=4) G.add_edge(3,4,color=”g”,weight=6) pos = nx.circular_layout(G) edges = G.edges() colors = … Read more

Can one get hierarchical graphs from networkx with python 3?

[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 … Read more