簡體   English   中英

Networkx:繪制邊緣某些屬性

[英]Networkx: draw edges certain attributes

使用Networkx,我正在考慮一個實際上是多個子圖的並集的圖。 我正在嘗試找出解決此問題的最佳方法。 目前,我一直在使用以下策略,但想知道是否有人有更好的建議。

首先,我一次添加一個節點,它們來自具有m行和n列的矩形網格,因此相當容易。

G=nx.Graph()
PN = set() #first set of nodes
DN = set() #second set of nodes

for i in range(m):
    for j in range(n):
        if <sorting criterion here>:
            G.add_node(j + i *n, pos = <insert function of i and j here>, 
                       node_type = 'type_1', node_color = 'b')
            PN.add(j + i *n, pos = <insert function of i and j here> )
        else:
            G.add_node(j + i *n, pos = <insert function of i and j here>, 
                        node_type = 'type_2', node_color = 'g')
            DN.add(j + i *n, pos = <insert function of i and j here> )

pos = nx.get_node_attributes(G, 'pos')
nx.draw_networkx_nodes(G,pos,PN, node_color = 'b', node_size = 35)
nx.draw_networkx_nodes(G,pos,DN, node_color = 'g', node_size = 35)

我已經嘗試過,但是沒有成功,做一些我認為會更好的類似事情。 這個想法如下:

G=nx.Graph()
V = set()

for i in range(m):
    for j in range(n):
        if <sorting criterion here>:
            V.add(j + i *n, pos = <insert function of i and j here>, 
                  node_type = 'type 1', node_color = 'b')
        else:
            V.add(j + i *n, pos = <insert function of i and j here>, 
                  node_type = 'type 2', node_color = 'g')

color = nx.get_node_attributes(G,'node_color')
pos = nx.get_node_attributes(G, 'pos')
nx.draw_networkx_nodes(G,pos, color)

不幸的是,“顏色”似乎不像“ pos”那樣工作,后一種方法輸出所有紅色節點。 任何建議將不勝感激!

您是對的-'node_color'參數采用單一顏色或顏色列表(而不是字典)。 因此,最簡單的方法是使用與第一個代碼段相同的策略,並分別繪制兩組節點。

如果只想發出一次對nx.draw_networkx_nodes()的調用,則node_color參數應具有與節點列表相同長度和相同順序的顏色列表。 因此,您可以執行以下操作:

color = nx.get_node_attributes(G,'node_color')
pos = nx.get_node_attributes(G, 'pos')
nodelist,node_color = zip(*color.items())
nx.draw_networkx_nodes(G,pos, nodelist=nodelist, node_color=node_color)

暫無
暫無

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

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