[英]Two Edges Between Nodes
我用權重做了一個圖表。 我在 Node1 和 Node2 之間有兩條邊。 我可以畫出它們的重量,但我看不到兩條邊。 如何繪制兩條邊? 它們的權重為 1 和 2。(Node2 到 Node1 = 1,Node1 到 Node2 = 2)
我的代碼:
import networkx as nx
from networkx.drawing.nx_agraph import write_dot
import matplotlib.pyplot as plt
G=nx.DiGraph()
i=1
#Adding nodes to graph
# "pos" is for the location of the nodes
G.add_node(0,pos=(0,5))
G.add_node(1,pos=(10,0))
G.add_node(2,pos=(5,-5))
G.add_node(3,pos=(-5,-5))
G.add_node(4,pos=(-10,0))
# Adding edges each node
G.add_edge(0,4,weight=2)
G.add_edge(0,1,weight=5)
G.add_edge(0,2,weight=3)
G.add_edge(1,3,weight=6)
G.add_edge(1,2,weight=2)
G.add_edge(2,1,weight=1,)
G.add_edge(2,3,weight=2)
G.add_edge(4,3,weight=4)
G.add_edge(4,2,weight=10)
G.add_edge(4,1,weight=6)
pos=nx.get_node_attributes(G,'pos')
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, label_pos=0.2, font_size=9)
list = [nx.dijkstra_path(G,4,1,6),nx.dijkstra_path(G,4,2,10),nx.dijkstra_path(G,4,3,4)]
nx.draw(G, pos, with_labels=True)
print("--------------------------------------")
print((list))
print("--------------------------------------")
print("Shortest path btwn 4-1:",nx.dijkstra_path(G,4,1),"=",nx.dijkstra_path_length(G,4,1))
print("Shortest path btwn 4-2:",nx.dijkstra_path(G,4,2),"=",nx.dijkstra_path_length(G,4,2))
print("Shortest path btwn 4-3:",nx.dijkstra_path(G,4,3),"=",nx.dijkstra_path_length(G,4,3))
print("--------------------------------------")
labels = nx.get_edge_attributes(G,'weight', )
print("Before removing Node 1: ",G.nodes)
#nx.draw_networkx_edge_labels(G,pos,edge_labels=labels, )
plt.show()
plt.clf() #Clears the current figure.
G.remove_node(1) # this line of code remove only vertex, don't forget also remove weights
labels = nx.get_edge_attributes(G,'weight', )
pos=nx.get_node_attributes(G,'pos')
print("--------------------------------------")
print("After removing Node1: ",G.nodes)
print("--------------------------------------")
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels, )
nx.draw(G, pos, with_labels=True)
plt.axis('off')
plt.show()
我的圖:
如何為 Node1 和 Node2 之間的每個權重創建一條邊?
編輯: 在 Python 中以分別顯示所有邊的方式繪制有向圖我嘗試使用nx.draw(G, connectionstyle='arc3, rad = 0.1')
但我得到了錯誤。
現在我再次檢查,發現我把 function 放錯了地方。 我將在下面回答。
nx.draw(G,pos, connectionstyle='arc3, rad = 0.09',with_labels=True ) `helped to solve the problem.`
import networkx as nx
from networkx.drawing.nx_agraph import write_dot
import matplotlib.pyplot as plt
G=nx.DiGraph()
i=1
#Adding nodes to graph
# "pos" is for the location of the nodes
G.add_node(0,pos=(0,5))
G.add_node(1,pos=(10,0))
G.add_node(2,pos=(5,-5))
G.add_node(3,pos=(-5,-5))
G.add_node(4,pos=(-10,0))
# Adding edges each node
G.add_edge(0,4,weight=2)
G.add_edge(0,1,weight=5)
G.add_edge(0,2,weight=3)
G.add_edge(1,3,weight=6)
G.add_edge(1,2,weight=2)
G.add_edge(2,1,weight=1,)
G.add_edge(2,3,weight=2)
G.add_edge(4,3,weight=4)
G.add_edge(4,2,weight=10)
G.add_edge(4,1,weight=6)
pos=nx.get_node_attributes(G,'pos')
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, label_pos=0.2, font_size=9)
list = [nx.dijkstra_path(G,4,1,6),nx.dijkstra_path(G,4,2,10),nx.dijkstra_path(G,4,3,4)]
# PRINTING BOTH EDGES BETWEEN TWO NODES
nx.draw(G,pos, connectionstyle='arc3, rad = 0.09',with_labels=True )
print("--------------------------------------")
print((list))
print("--------------------------------------")
print("Shortest path btwn 4-1:",nx.dijkstra_path(G,4,1),"=",nx.dijkstra_path_length(G,4,1))
print("Shortest path btwn 4-2:",nx.dijkstra_path(G,4,2),"=",nx.dijkstra_path_length(G,4,2))
print("Shortest path btwn 4-3:",nx.dijkstra_path(G,4,3),"=",nx.dijkstra_path_length(G,4,3))
print("--------------------------------------")
labels = nx.get_edge_attributes(G,'weight', )
print("Before removing Node 1: ",G.nodes)
plt.show()
plt.clf() #Clears the current figure.
G.remove_node(1) # this line of code remove only vertex, don't forget also remove weights
labels = nx.get_edge_attributes(G,'weight', )
pos=nx.get_node_attributes(G,'pos')
print("--------------------------------------")
print("After removing Node1: ",G.nodes)
print("--------------------------------------")
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels, )
nx.draw(G, pos, with_labels=True)
plt.axis('off')
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.