图表中边缘标签的正确位置
Correct position of edge labels in a graph plot
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
a = np.reshape(np.random.random_integers(0,1,size=25),(5,5)) # random 5,5 numpy-array
np.fill_diagonal(a,0) # remove self-loops
print "np-array: \n" + str(a)
D = nx.DiGraph(a) # create directional graph from numpy array
weighted_edges = dict(zip(D.edges(),np.random.randint(1,10,size=len(D.edges())))) # assign random weights to each edge
edge_tuple_list = [(key[0],key[1],value) for key,value in zip(weighted_edges.keys(),weighted_edges.values())]
D.add_weighted_edges_from(edge_tuple_list) #convert to list of edge tuples and add to the graph
nx.draw(D,with_labels=True,pos=nx.spring_layout(D),node_size=700) #draw the graph
nx.draw_networkx_edge_labels(D,pos=nx.spring_layout(D),edge_labels=nx.get_edge_attributes(D,'weight')) #add edge labels
生成下图。如何修正边标签的位置?
你的问题是,当你调用 pos=nx.spring_layout(G)
时,它会重新计算 pos
,它从随机位置开始,然后迭代更新它们。最终结果不是唯一的——根据初始值,结果可能大不相同。所以你第一次绘制网络时它计算了一些位置,但是当你放置边缘标签时它计算了新位置。
所以你想创建一个 pos
字典,它将始终保存相同的位置。我只更改了下面代码的最后 3 行。
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
a = np.reshape(np.random.random_integers(0,1,size=25),(5,5)) # random 5,5 numpy-array
np.fill_diagonal(a,0) # remove self-loops
print "np-array: \n" + str(a)
D = nx.DiGraph(a) # create directional graph from numpy array
weighted_edges = dict(zip(D.edges(),np.random.randint(1,10,size=len(D.edges())))) # assign random weights to each edge
edge_tuple_list = [(key[0],key[1],value) for key,value in zip(weighted_edges.keys(),weighted_edges.values())]
D.add_weighted_edges_from(edge_tuple_list) #convert to list of edge tuples and add to the graph
pos = nx.spring_layout(D) # <---this line is new. the pos here replaces nx.spring_layout below.
nx.draw(D, pos=pos, with_labels=True, node_size=700) #draw the graph
nx.draw_networkx_edge_labels(D, pos=pos, edge_labels=nx.get_edge_attributes(D,'weight')) #add edge labels
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
a = np.reshape(np.random.random_integers(0,1,size=25),(5,5)) # random 5,5 numpy-array
np.fill_diagonal(a,0) # remove self-loops
print "np-array: \n" + str(a)
D = nx.DiGraph(a) # create directional graph from numpy array
weighted_edges = dict(zip(D.edges(),np.random.randint(1,10,size=len(D.edges())))) # assign random weights to each edge
edge_tuple_list = [(key[0],key[1],value) for key,value in zip(weighted_edges.keys(),weighted_edges.values())]
D.add_weighted_edges_from(edge_tuple_list) #convert to list of edge tuples and add to the graph
nx.draw(D,with_labels=True,pos=nx.spring_layout(D),node_size=700) #draw the graph
nx.draw_networkx_edge_labels(D,pos=nx.spring_layout(D),edge_labels=nx.get_edge_attributes(D,'weight')) #add edge labels
生成下图。如何修正边标签的位置?
你的问题是,当你调用 pos=nx.spring_layout(G)
时,它会重新计算 pos
,它从随机位置开始,然后迭代更新它们。最终结果不是唯一的——根据初始值,结果可能大不相同。所以你第一次绘制网络时它计算了一些位置,但是当你放置边缘标签时它计算了新位置。
所以你想创建一个 pos
字典,它将始终保存相同的位置。我只更改了下面代码的最后 3 行。
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
a = np.reshape(np.random.random_integers(0,1,size=25),(5,5)) # random 5,5 numpy-array
np.fill_diagonal(a,0) # remove self-loops
print "np-array: \n" + str(a)
D = nx.DiGraph(a) # create directional graph from numpy array
weighted_edges = dict(zip(D.edges(),np.random.randint(1,10,size=len(D.edges())))) # assign random weights to each edge
edge_tuple_list = [(key[0],key[1],value) for key,value in zip(weighted_edges.keys(),weighted_edges.values())]
D.add_weighted_edges_from(edge_tuple_list) #convert to list of edge tuples and add to the graph
pos = nx.spring_layout(D) # <---this line is new. the pos here replaces nx.spring_layout below.
nx.draw(D, pos=pos, with_labels=True, node_size=700) #draw the graph
nx.draw_networkx_edge_labels(D, pos=pos, edge_labels=nx.get_edge_attributes(D,'weight')) #add edge labels