ive created the dictionary for the diagram
dict_exports= {}
for index,x in myDataTrade.iterrows():
dict_exports.setdefault(x['from'],0.) # again we use setdefault!
dict_exports[x['from']]+=float(x['VAL'])
and the network plot which runs fine but does not change the weight to the nodes.
# let's change a bit the function to use this new info
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() # create the empty object
# this allows us to loop on the values of the dataframe
for index,x in myDataTradeSWE.iterrows():
G.add_edge(x['from'],x['to'],w=x['VAL'])
color_code=[] # this is the list for edges in this case
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.) # we divide by 1M just to make it visible
# let's plot it
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)
the dictionary will not run properly. how can i get the dictionary to integrate with the rest of the code.