簡體   English   中英

如何 select 在 networkx.draw 中繪制邊緣列表

[英]How to select a list of edges to draw in networkx.draw

我有一個帶有許多邊的 networkx 圖,因此我想 select 一個我想繪制的子集。 但是有奇怪的行為。

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)],
       )

這就是結果。 (我稍后會添加一個顏色條,因此可以解釋 colors)。

繪制的圖像

# 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),],
       )

有沒有辦法防止這個錯誤? 在我看來,這是錯誤。 但也許有一些我想念的東西。

我發現了錯誤:您還必須編輯顏色關鍵字參數:

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,
       )

但我仍然認為我得到的錯誤沒有幫助......

你設置colors = nx.get_edge_attributes(G,'color').values()

這給出了dict_values([0.1, 5.7, 0.4, 1.4, 2.4])

draw試圖將 5 個值與 3 個邊匹配

所以就像你說的,你必須調整colors字典的大小

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM