pyvis:有没有办法禁用物理效果而不失去图形布局?

4
我正在尝试使用pyvis可视化一个大型网络,并遇到两个问题:
  1. 渲染时间过长
  2. 网络不稳定,即节点移动太快,很难与这样的网络进行交互。
通过使用toggle_physics(False)禁用物理特性可以加速渲染并使网络静态化,但会导致布局设置被消除。禁用物理特性后它看起来像这样: link。如您所见,图形混乱且没有结构。
我想要做的是禁用物理特性,但保留布局设置,即我希望我的图形看起来像普通网络(例如networkX中的弹簧布局),并考虑每条边的权重。有没有办法实现这一点?
到目前为止,我发现pyvis只有分层布局,这不是我需要的。我认为整合networkX布局可能会有所帮助,但我不知道该怎么做,因为networkX允许在nx.draw()函数中将布局设置为关键字参数,这与我的情况不兼容。以下是我的代码,如果有助于理解我的问题:
g = nx.Graph()
edges_cards = cards_weights_df.values.tolist()
g.add_weighted_edges_from(edges_cards)

net = Network("1000px", "1800px")
net.from_nx(g)
net.toggle_physics(False)
net.show("graph.html")

感谢您的帮助!

1个回答

5

您可以将xy坐标传递给pyvis节点(请参见此处的文档)。 然后,您可以使用networkx创建图形布局,并将结果位置传递给pyvis图形。下面是一个示例,应用了nx.circular_layout()karate俱乐部网络

import networkx as nx
from pyvis.network import Network

G = nx.karate_club_graph()
pos=nx.circular_layout(G,scale=500)

net = Network()
net.from_nx(G)

for node in net.get_nodes():
  net.get_node(node)['x']=pos[node][0]
  net.get_node(node)['y']=-pos[node][1] #the minus is needed here to respect networkx y-axis convention 
  net.get_node(node)['physics']=False
  net.get_node(node)['label']=str(node) #set the node label as a string so that it can be displayed

net.toggle_physics(False)
net.show('net.html')

这里是圆形布局的结果: enter image description here 没有任何特定的布局:
import networkx as nx
from pyvis.network import Network

G = nx.karate_club_graph()

net = Network()
net.from_nx(G)

for node in net.get_nodes():
  net.get_node(node)['physics']=False
  net.get_node(node)['label']=str(node)

net.toggle_physics(False)
net.show('net.html')

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接