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 ?
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.
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.
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.
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.
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
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
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With