如何在 networkx 中绘制不重叠的边缘标签?

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

如何在 networkx 中绘制不重叠的边缘标签?使用选项 scale 看起来更好,但边缘标签仍然重叠,例如,

相关源码如下:

# 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)

这是 spring_layout 的 documentation。其中一个参数是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.

所以用 k=5/math.sqrt(G.order()) 或其他会增加距离的值来调用 spring_layout