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,
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)
You can use draw_networkx_edge_labels(edge_labels) to draw label between edges. If edge_labels is not given, the attributes of edge is used. edge_labels should be a dictionary keyed by edge two-tuple of text labels. Only labels for the keys in the dictionary are drawn.
nbunch. An nbunch is a single node, container of nodes or None (representing all nodes). It can be a list, set, graph, etc.. To filter an nbunch so that only nodes actually in G appear, use G.
There are two options: Change the force law (edit the networkx code), or graph the components separately. However the "best" way to deal with this is to chart the components seperately. You can iterate over the components, charting them in seperate plots, with the functions described here.
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.
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