Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to draw multigraph in networkx using matplotlib or graphviz

Tags:

when I pass multigraph numpy adjacency matrix to networkx (using from_numpy_matrix function) and then try to draw the graph using matplotlib, it ignores the multiple edges.

how can I make it draw multiple edges as well ?

like image 716
Binary Baba Avatar asked Feb 18 '13 18:02

Binary Baba


People also ask

What is MultiGraph in NetworkX?

MultiGraph (data=None, **attr)[source] An undirected graph class that can store multiedges. Multiedges are multiple edges between two nodes. Each edge can hold optional data or attributes. A MultiGraph holds undirected edges.

How do you visualize a NetworkX graph?

Option 1: NetworkX NetworkX has its own drawing module which provides multiple options for plotting. Below we can find the visualization for some of the draw modules in the package. Using any of them is fairly easy, as all you need to do is call the module and pass the G graph variable and the package does the rest.

How do you create a directed graph in Python?

Add the nodes from any container (a list, dict, set or even the lines from a file or the nodes from another graph). In addition to strings and integers any hashable Python object (except None) can represent a node, e.g. a customized node object, or even another Graph. Edges: G can also be grown by adding edges.

How do I use NetworkX in Python?

Create Graph Now you use the edge list and the node list to create a graph object in networkx . Loop through the rows of the edge list and add each edge and its corresponding attributes to graph g . Similarly, you loop through the rows in the node list and add these node attributes.


2 Answers

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')  In [6]: !neato -T png multi.dot > multi.png 

enter image description here

On NetworkX 1.11 and newer, nx.write_dot doesn't work as per issue on networkx github. The workaround is to call write_dot using

from networkx.drawing.nx_pydot import write_dot

or

from networkx.drawing.nx_agraph import write_dot

like image 164
Aric Avatar answered Sep 18 '22 14:09

Aric


You can use matplotlib directly using the node positions you calculate.

G=nx.MultiGraph ([(1,2),(1,2),(1,2),(3,1),(3,2)]) pos = nx.random_layout(G) nx.draw_networkx_nodes(G, pos, node_color = 'r', node_size = 100, alpha = 1) ax = plt.gca() for e in G.edges:     ax.annotate("",                 xy=pos[e[0]], xycoords='data',                 xytext=pos[e[1]], textcoords='data',                 arrowprops=dict(arrowstyle="->", color="0.5",                                 shrinkA=5, shrinkB=5,                                 patchA=None, patchB=None,                                 connectionstyle="arc3,rad=rrr".replace('rrr',str(0.3*e[2])                                 ),                                 ),                 ) plt.axis('off') plt.show() 

enter image description here

like image 37
atomh33ls Avatar answered Sep 19 '22 14:09

atomh33ls