给节点增加权重

Adding weight to nodes

我已经为节点的权重定义了字典:

  dict_exports={}

for index,x in myDataTrade.iterrows():
        dict_exports.setdefault(x['from'],0.)
        dict_exports[x['from']]+=float(x['VAL'])
def plot_sub_graph_trade(COUNTRY,FILTER,dict_exports,myDataTrade,FILTER_YEAR,FILTER_TYPE):

    myDataTradeSWE = myDataTrade[(myDataTrade['from']==COUNTRY) & (myDataTrade['VAL']>FILTER) & (myDataTrade['Year']==FILTER_YEAR) & (myDataTrade['type']==FILTER_TYPE)]

    G=nx.Graph() 

    
    for index,x in myDataTradeSWE.iterrows():
        G.add_edge(x['from'],x['to'],w=x['VAL'])

    color_code=[]  
    for i in G.edges():
        color_code.append(G[i[0]][i[1]]['w'])  

    nodes_label={}
    n_size=[]
    for i in G.nodes():
        nodes_label[i]=str(i)
        n_size.append(dict_exports[i]/1000000.) 

    plt.figure(1,figsize=(8,8)) 
    plt.title('High Tech Trade Network of '+COUNTRY+' in '+str(FILTER_YEAR))
    nx.draw(G, pos=nx.kamada_kawai_layout(G),alpha=0.9,
            node_size=500,width=4,edge_cmap=plt.cm.Purples,edge_color=color_code,
            node_color='#A0CBE2',labels=nodes_label,font_size=11,font_color='Black')
COUNTRY='SWE'
FILTER=250000
FILTER_YEAR=2016
FILTER_TYPE= 'High_Tech'
plot_sub_graph_trade(COUNTRY,FILTER,dict_exports,myDataTrade,FILTER_YEAR,FILTER_TYPE)

代码returns网络图但没有集成字典,所以权重不影响节点。我不确定我是否错过了错误?请帮助。

在您对 nx.draw 的调用中,您仍然拥有固定的节点大小。我猜你想要

nx.draw(G, pos=nx.kamada_kawai_layout(G), alpha=0.9,
        node_size=n_size, 
        width=4, edge_cmap=plt.cm.Purples, edge_color=color_code,
        node_color='#A0CBE2', labels=nodes_label, font_size=11, font_color='Black')