Search code examples
python-3.xpandasgraphnetworkxpyvis

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?


Solution

  • 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()