Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 546
SparkAndShine Avatar asked Jan 06 '16 10:01

SparkAndShine


People also ask

How do I add labels to edges in NetworkX?

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.

What is Nbunch in NetworkX?

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.

How do you increase distance between nodes on NetworkX?

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.


1 Answers

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.

like image 146
Joel Avatar answered Sep 18 '22 21:09

Joel