Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyvis network keeps on moving

I have a text corpus for which I want to visualize the co-occurence of words as a network. To do so, I have created a pd Dataframe cooc_pd with the columns Source,Target and Weight. The first two are nodes and Weight indicates how often the two nodes (words) occur within a set window_size.

Then, I use the following code to plot a network:

import networkx as nx
from pyvis.network import Network
import pandas as pd

G = nx.from_pandas_edgelist(cooc_pd, 
                            source = 'Source', 
                            target = 'Target',
                            edge_attr='Weight')

net = Network(notebook=True)
net.from_nx(G)
net.show("example.html")
 

If I choose a low threshold for weight inclusion, many connections are shown in the graph. However, in that case the nodes in the example.html are constantly moving and interpretation is difficult. Is there a way (other then increasing the threshold) to make the nodes stop moving?

like image 574
Emil Avatar asked Jul 15 '26 00:07

Emil


2 Answers

I was having the same problem with my graph, it kept moving in a noisy way.

Reading the documentation, I've found a method called repulsion, which "Set the physics attribute of the entire network to repulsion".

Right after creating the Network, I've inserted this and it worked fine:

from pyvis.network import Network

net = Network()
net.repulsion()
like image 112
senajoaop Avatar answered Jul 17 '26 16:07

senajoaop


You can use

net.show_buttons(filter_=['physics']) 

to manage physical parameters using sliders in the visualization.

Read more in the PyVis docs here.

like image 28
Sara Avatar answered Jul 17 '26 17:07

Sara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!