我有一個帶有許多邊的 networkx 圖,因此我想選擇一個要繪制的子集。但是有奇怪的行為。
import networkx as nx
G = nx.Graph()
G.add_edge(0,1,color=.1,weight=2)
G.add_edge(1,2,color=.4,weight=4)
G.add_edge(2,3,color=1.4,weight=6)
G.add_edge(3,4,color=2.4,weight=3)
G.add_edge(4,0,color=5.7,weight=1)
colors = nx.get_edge_attributes(G,'color').values()
weights = nx.get_edge_attributes(G,'weight').values()
pos = nx.circular_layout(G)
# This works:
nx.draw(G, pos,
edge_color=colors,
width=list(weights),
with_labels=True,
node_color='lightgreen',
)
# This works too:
nx.draw(G, pos,
edge_color=colors,
width=list(weights),
with_labels=True,
node_color='lightgreen',
edgelist=[(0,1),(1,2),(2,3),(3,4),(4,0)],
)
這就是結果。(我稍后會添加一個顏色條,以便可以解釋顏色)。

# This however gives an error:
# ValueError: Invalid RGBA argument: 0.1
nx.draw(G, pos,
edge_color=colors,
width=list(weights),
with_labels=True,
node_color='lightgreen',
edgelist=[(0,1),(1,2),(2,3),],
)
有沒有辦法防止這個錯誤?在我看來,這是錯誤。但也許有一些我想念的東西。
uj5u.com熱心網友回復:
你設定 colors = nx.get_edge_attributes(G,'color').values()
這給 dict_values([0.1, 5.7, 0.4, 1.4, 2.4])
draw 正在嘗試將 5 個值與 3 個邊匹配
所以就像你說的,你必須調整colors字典的大小
uj5u.com熱心網友回復:
我發現了錯誤:您還必須編輯顏色關鍵字引數:
edgelist=[(0,1),(1,2),(2,3),]
nx.draw(G, pos,
edge_color=[G.edges[e]['color'] for e in edgelist],
width=list(weights),
with_labels=True,
node_color='lightgreen',
edgelist=edgelist,
)
但我仍然認為我得到的錯誤沒有幫助......
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/415777.html
標籤:
下一篇:如何將繪圖保存到新檔案夾
