10

How do I draw non-overlapping edge labels in networkx? Using the option scale looks better but the edge labels are still overlapping, for instance,

enter image description here

The related source codes are below:

# build a graph
G.add_edge(u, v, r=value)

# plot the graph
pos = nx.spring_layout(G, scale=3)

nx.draw(G, pos)

edge_labels = nx.get_edge_attributes(G,'r')
nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_labels)

plt.savefig(filename)
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134

1 Answers1

8

Here is the documentation for spring_layout. One of the parameters is k.

k (float (default=None)) – Optimal distance between nodes. If None the distance is set to 1/sqrt(n) where n is the number of nodes. Increase this value to move nodes farther apart.

So call spring_layout with k=5/math.sqrt(G.order()) or something other value that will increase the distance.

Joel
  • 22,598
  • 6
  • 69
  • 93
  • 6
    thx. it is not scalable (it looks worse for big graphs). It might be a good solution to use other graph visualization tools, like Graphviz. – SparkAndShine Jan 06 '16 at 17:06