簡體   English   中英

縮放與鄰接矩陣成正比的 NetworkX 節點和邊

[英]Scaling NetworkX nodes and edges proportional to adjacency matrix

NetworkX 是否有一種內置的方式來縮放與鄰接矩陣頻率/節點-節點頻率成比例的節點和邊? 我試圖根據鄰接矩陣頻率和基於節點-節點頻率的邊權重來縮放節點和文本的大小。 我為圖形創建了一個頻率屬性,但這並不能解決我將有關節點-節點頻率的信息傳遞給圖形的問題。

所以兩部分問題:
1) 將鄰接矩陣轉換為 networkX 圖的最佳實踐是什么?
2)我如何使用該信息來縮放節點的大小和邊的權重?

## Compute Graph (G)
G = nx.Graph(A)

## Add frequency of word as attribute of graph
def Freq_Attribute(G, A):
    frequency = {}  # Dictionary Declaration
    for node in G.nodes():
        frequency[str(node)] = A[str(node)][str(node)]
    return nx.set_node_attributes(G, 'frequency', frequency)

Freq_Attribute(g,A) # Adds attribute frequency to graph, for font scale

## Plot Graph with Labels
plt.figure(1, figsize=(10,10))

# Set location of nodes as the default
pos = nx.spring_layout(G, k=0.50, iterations=30)  

# Nodes
node_size = 10000
nodes1 = nx.draw_networkx_nodes(G,pos,
                       node_color='None',
                       node_size=node_size,
                       alpha=1.0)  # nodelist=[0,1,2,3],
nodes1.set_edgecolor('#A9C1CD') # Set edge color to black

# Edges
edges = nx.draw_networkx_edges(G,pos,width=1,alpha=0.05,edge_color='black')
edges.set_zorder(3)

# Labels
nx.draw_networkx_labels(G,pos,labels=nx.get_node_attributes(G,'label'),
                        font_size=16, 
                        font_color='#062D40',
                        font_family='arial')  # sans-serif, Font=16
# node_labels = nx.get_node_attributes(g, 'name') 
# Use 'g.graph' to find attribute(s): {'name': 'words'}

plt.axis('off')
#plt.show()

我試過設置標簽 font_size,但這不起作用。:font_size=nx.get_node_attributes(G,'frequency')) + 8)

我嘗試了以下方法來滿足您的需求:

import networkx as nx
import matplotlib.pyplot as plt

## create nx graph from adjacency matrix
def create_graph_from_adj(A):
    # A=[(n1, n2, freq),....]
    G = nx.Graph()
    for a in A:
        G.add_edge(a[0], a[1], freq=a[2])
    return G

A = [(0, 1, 0.5), (1, 2, 1.0), (2, 3, 0.8), (0, 2, 0.2), (3, 4, 0.1), (2, 4, 0.6)]
## Compute Graph (G)
G = create_graph_from_adj(A)

plt.subplot(121)

# Set location of nodes as the default
spring_pose = nx.spring_layout(G, k=0.50, iterations=30)  

nx.draw_networkx(G,pos=spring_pose)


plt.subplot(122)
# Nodes
default_node_size = 300
default_label_size = 12
node_size_by_freq = []
label_size_by_freq = []
for n in G.nodes():
    sum_freq_in = sum([G.edge[n][t]['freq'] for t in G.neighbors(n)])
    node_size_by_freq.append(sum_freq_in*default_node_size)
    label_size_by_freq.append(int(sum_freq_in*default_label_size))

nx.draw_networkx_nodes(G,pos=spring_pose,
                       node_color='red',
                       node_size=node_size_by_freq,
                       alpha=1.0)  
nx.draw_networkx_labels(G,pos=spring_pose,
                        font_size=12,  #label_size_by_freq is not allowed
                        font_color='#062D40',
                        font_family='arial') 

# Edges
default_width = 5.0
edge_width_by_freq = []
for e in G.edges():
    edge_width_by_freq.append(G.edge[e[0]][e[1]]['freq']*default_width)
nx.draw_networkx_edges(G,pos=spring_pose,
                       width=edge_width_by_freq,
                       alpha=1.0,
                       edge_color='black')

plt.show()

在此處輸入圖片說明

首先,鄰接反應不是以矩陣格式給出的,但恕我直言,這太乏味了。

其次, nx.draw_networkx_labels不允許標簽的字體大小不同。 幫不上忙。

最后,邊緣寬度和節點大小允許這樣做。 因此,它們分別根據其頻率和輸入頻率的總和進行縮放。

希望能幫助到你。

暫無
暫無

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

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