Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Networkx: Overlapping edges when visualizing MultiGraph

The following multigraph plots correctly (i.e. parallel edges do not overlap) using graphviz neato to generate a png (as shown in this answer)

import networkx as nx
nx.MultiGraph ([(1,2),(1,2),(1,2),(3,1),(3,2)])
nx.write_dot(Gm,'multi.dot')
!neato -T png multi.dot > multi.png

However using the draw function of Networkx doesn't do the trick

nx.draw_graphviz(Gm,prog='neato')

Is it possible to prevent overlapping edges using the draw methods from Networkx?

Thanks

like image 587
JuanPi Avatar asked Feb 24 '13 16:02

JuanPi


People also ask

Can NetworkX handle large graphs?

For NetworkX, a graph with more than 100K nodes may be too large. I'll demonstrate that it can handle a network with 187K nodes in this post, but the centrality calculations were prolonged. Luckily, there are some other packages available to help us with even larger graphs.

What is a DiGraph in NetworkX?

A DiGraph stores nodes and edges with optional data, or attributes. DiGraphs hold directed edges. Self loops are allowed but multiple (parallel) edges are not. Nodes can be arbitrary (hashable) Python objects with optional key/value attributes. By convention None is not used as a node.

How can you tell if a graph is directed by NetworkX?

To check if the graph is directed you can use nx.is_directed(G) , you can find the documentation here. 'weight' in G[1][2] # Returns true if an attribute called weight exists in the edge connecting nodes 1 and 2.


1 Answers

You can use matplotlib directly using the node positions you have calculated.

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 183
atomh33ls Avatar answered Sep 28 '22 02:09

atomh33ls