Pyvis network keeps on moving
Asked Answered
R

3

7

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?

Roee answered 24/6, 2021 at 14:20 Comment(1)
PyVis is a rather new package that looks very exciting. I had, personally, not heard of it before, but I am just wondering if the constant movement, has something to do with a layout algorithm, keep being executed. Have you checked the parameters for Physics, perhaps trying to totally deactivate.Ea
R
13

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()
Rode answered 3/7, 2022 at 17:13 Comment(0)
P
2

You can use

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

to manage physical parameters using sliders in the visualization.

Read more in the PyVis docs here.

Parclose answered 29/1, 2022 at 12:41 Comment(0)
B
0

The problem is that toggle_physics(False) doesn't allow the generated html to initially group the nodes out using a physics based algorithm like force Atlas 2based. So what we really want to do is disable physics right after the G.show_buttons(filter_=['physics']) and immediately toggling physics off, but a more robust solution would be editing the html itself so that physics is automatically turned off after everything has loaded. This can be done by calling the following function:

import re

def add_physics_stop_to_html(filepath):
    with open(filepath, 'r', encoding="utf-8") as file:
        content = file.read()

    # Search for the stabilizationIterationsDone event and insert the network.setOptions line
    pattern = r'(network.once\("stabilizationIterationsDone", function\(\) {)'
    replacement = r'\1\n\t\t\t\t\t\t  // Disable the physics after stabilization is done.\n\t\t\t\t\t\t  network.setOptions({ physics: false });'

    new_content = re.sub(pattern, replacement, content, flags=re.DOTALL)

    # Write the modified content back to the file
    with open(filepath, 'w', encoding="utf-8") as file:
        file.write(new_content)

# Example usage
nt.write_html('nx.html')
add_physics_stop_to_html("nx.html")

Bores answered 24/10, 2023 at 19:21 Comment(1)
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From ReviewWendeline

© 2022 - 2024 — McMap. All rights reserved.