How do I draw non-overlapping edge labels in networkx?

SparkAndShine picture SparkAndShine · Jan 6, 2016 · Viewed 8.4k times · Source

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)

Answer

Joel picture Joel · Jan 6, 2016

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.