0

I have data in a networkX graph where every edge has a "Type", using pyviz I want to change the color of the edge according to the category. Here is a sample code, I first set a color map:

color_map=nx.get_edge_attributes(G,'Type')
for key in color_map:
    if color_map[key] == 'Services':
        color_map[key]='blue'
    if color_map[key]== 'Goods':
        color_map[key]='red'
    if color_map[key]=='Taxes':
        color_map[key]='black'
    if color_map[key]=='Other':
        color_map[key]='white'

And the pyviz code is as follows:

scale=1 #scaling the size of the nodes by X*degree
d.update((x, scale*y) for x, y in d.items()) #update dictionary
nx.set_node_attributes(G,d,'size')
#net=Network(notebook=True)
net = Network(height="1500px", width="100%", bgcolor="#222222", font_color="white",directed=True,select_menu=True,filter_menu=False,neighborhood_highlight=True,heading="Sample")
net.from_nx(G,show_edge_weights=True)
net.barnes_hut(gravity=-16750, central_gravity=0.35, spring_length=0, spring_strength=0.015, damping=1, overlap=1)
net.show_buttons(filter_=['physics'])
for e in net.edges:
    e['width'] = e['Weight']*0.0000001 #Pyvis will look for width, so remember to replace with whatever you have in your DF
    e['color'] = color_map[node['Type']]
net.show('Sample1.html',local=False)

I get a KeyError:'Type' when I run it. Any help would be appreciated!

0 Answers0