1
我正在繪製具有邊緣權重的網絡x圖形,我想累計累加權重。下面的代碼只獲取邊的最後一個權重,但累計和。有5個節點和3個邊。邊緣爲('A', 'B'), ('A', 'D')
,('C', 'E')
,權重爲[1, 1, 1]
。我想要的重量是[2, 2, 1]
而不是[1, 1, 1]
。需要幫忙。韓國社交協會。在累積和的邊緣上的網絡x權重
1.創建DF
import pandas as pd
import networkx as nx
ints = [1] * 5
a = ['A', 'B', 'C', 'A', 'A']
b = ['D', 'A', 'E', 'D', 'B']
df = pd.DataFrame(ints, columns=['weight'])
df['a'] = a
df['b'] = b
df
weight a b
0 1 A D
1 1 B A
2 1 C E
3 1 A D
4 1 A B
2.繪製的曲線圖。
G=nx.from_pandas_dataframe(df, 'a', 'b', ['weight'])
edges = G.edges()
weights = [G[u][v]['weight'] for u,v in edges]
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True, width=weights)
很棒,@Mikk,TKS。 –