Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

edge length in networkx

I am trying to adjust the length of edge between two nodes by following code. But apparently it didn't work. Could anyone guide me where I am making mistake: Please note that I already look at this thread (How to specify edge length in Networkx for calculating shortest distance?) but didn't solve my issue

import networkx as nx
import matplotlib.pyplot as plt

G=nx.Graph()
G.add_nodes_from([1,2])
G.add_edge(1,2, length = 10)  # I also replaced length with len but no luck
nx.draw(G,with_labels=True)
plt.show() # display
like image 281
user2293224 Avatar asked Oct 17 '17 06:10

user2293224


People also ask

Does NetworkX have edge?

Returns True if the graph has an edge between nodes u and v. This is the same as v in G[u] or key in G[u][v] without KeyError exceptions.

Can NetworkX handle large graphs?

For NetworkX, a graph with more than 100K nodes may be too large. I'll demonstrate that it can handle a network with 187K nodes in this post, but the centrality calculations were prolonged. Luckily, there are some other packages available to help us with even larger graphs.

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.


1 Answers

How about this:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_nodes_from([1,2])
G.add_edge(1,2, length = 10)
pos = nx.spring_layout(G)
nx.draw(G, pos)
nx.draw_networkx_edge_labels(G, pos)
plt.show()

It will look like this:

enter image description here

You can also play around with draw_networkx_edge_labels's parameters to print out just exactly what you want.

like image 195
bergerg Avatar answered Oct 11 '22 16:10

bergerg