peterm
peterm

Reputation: 23

Width of edges working in networkx, but not in pyvis

I'm plotting a network from a pandas df using Networkx and pyvis. The edge weights are working in Networkx, but are not refelected in pyvis. Here is my code example:

import numpy as np; np.random.seed(0)
import pandas as pd
import networkx as nx
from pyvis.network import Network

file = r"test.xlsx"
df = pd.read_excel(file, engine='openpyxl')
node1 node2 node1_len node1_size edge_weight
0 AAAAAAAAAA AAAAAAAAAB 10 20 9.0
1 AAAAAAAAAA AAAAABBBBB 10 20 0.5
2 AAAAAAAAAB AAAAAAAAAA 10 40 9.0
3 AAAAAAAAAB AAAAABBBBB 10 40 0.6
4 AAAAABBBBB AAAAAAAAAA 10 60 0.5
5 AAAAABBBBB AAAAAAAAAB 10 60 0.6
#generate a Networkx graph
G = nx.from_pandas_edgelist(df, source = 'node1', target = 'node2', edge_attr = 'edge_weight', create_using = nx.Graph())

edges = G.edges.data()

#node positions
pos = nx.kamada_kawai_layout(G)

#setting up attributes
node_size = {}
#edge_width = {}

for index, row in df.iterrows():
    node_size[row['node1']] = row['node1_size']
    #edge_width[(row['node1'], row['node2'])] = row['edge_weight']

nx.set_node_attributes(G, node_size, 'size')
#nx.set_edge_attributes(G, edge_weight, 'ratio')

#draw networkx graph
edge_width = [e[2]['edge_weight'] for e in edges]
nx.draw(G, pos=pos, with_labels=True) # draw nodes (and edges!)
nx.draw_networkx_edges(G, pos=pos, width=edge_width) # paint over edges with specified width.

networkx graph

#Graph visualization using Pyvis
net = Network(width='1900px', height='900px', bgcolor='#222222', font_color='white')
net.repulsion()
net.from_nx(G)
for e in edges:
    print(e[2]['edge_weight'])
    net.add_edge(e[0],e[1], value=e[2]['edge_weight'])
net.show('test.html')`

pyvis graph

Notes:

The code lines where I'm trying to add the edge weights to the pyvis net object have no effect.

for e in edges:
    print(e[2]['edge_weight'])
    net.add_edge(e[0],e[1], value=e[2]['edge_weight'])

I tried various approaches, e.g., modifying the edge attributes in various ways in the networkx G object - like I did for the node sizes (see the commented-out lines in the '#setting up attributes' section), but didn't manage to change the edge width in pyvis.

Upvotes: 2

Views: 2895

Answers (2)

peterm
peterm

Reputation: 23

I have managed to solve the problem by changing this:

#Graph visualization using Pyvis
net = Network(width='1900px', height='900px', bgcolor='#222222', font_color='white')
net.repulsion()
net.from_nx(G)
for e in edges:
    print(e[2]['edge_weight'])
    net.add_edge(e[0],e[1], value=e[2]['edge_weight'])
net.show('test.html')

to this:

#Graph visualization using Pyvis
net = Network(width='1900px', height='900px', bgcolor='#222222', font_color='white')
net.repulsion()
net.from_nx(G)
for e in net.edges:
    e['width'] = e['edge_weight']
net.show('test.html')

Upvotes: 0

jylls
jylls

Reputation: 4705

If you use "width" (instead of "edge_weight") as the name of your width attribute while setting up your network with networkx, then pyvis will automatically interpret this attribute as the width of the edges.

See example below:

import networkx as nx
from pyvis.network import Network

G=nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_edge(1,2,width=30)
G.add_edge(2,3,width=1)
nx.draw(G,width=[e[2]['width'] for e in G.edges(data=True)])

enter image description here

net = Network(width='1900px', height='900px', bgcolor='#222222', font_color='white')
net.repulsion()
net.from_nx(G)
net.show('network.html')
display(HTML("network.html"))

enter image description here

Upvotes: 0

Related Questions