From the documentation of draw_networkx:
draw_networkx(G, pos=None, arrows=True, with_labels=True, **kwds)
Parameters:
[...]
pos (dictionary, optional) – A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will
be computed. See networkx.layout for functions that compute node
positions.
So, if you do not pass pos
explicitly, a spring_layout is generated, but this won't be identical to the layout that you generate through
pos = nx.spring_layout(G)
, because calling nx.spring_layout(G) twice gives different results:
for a in [0,1]:
pos = nx.spring_layout(G)
print(pos)
output:
{'A': array([ 0.65679786, -0.91414348]), 'B': array([0.34320214, 0.5814527 ]), 'C': array([-1. , 0.33269078])}
{'A': array([-0.85295569, -0.70179415]), 'B': array([ 0.58849111, -0.29820585]), 'C': array([0.26446458, 1. ])}
So, passing the same pos
to both drawing functions solves the problem:
pos = nx.spring_layout(G)
weights = nx.get_edge_attributes(G, "weight")
nx.draw_networkx(G, pos, with_labels=True)
nx.draw_networkx_edge_labels(G, pos, edge_labels=weights)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…