我正在使用 PyVis 进行网络可视化,并希望在悬停在节点上时在工具提示功能中添加一些额外的项目。
我基本上直接使用来自 GoT 网络的 PyVis 文档的教程部分的代码: https ://pyvis.readthedocs.io/en/latest/tutorial.html
此功能中的工具提示已设置,以便在将鼠标悬停在节点上时显示相邻邻居的列表。我想显示相同的信息,但也想显示每个邻居的相关边权重。
我知道在我的可视化中考虑了重量,因为边缘宽度会根据重量而变化,但我所做的几次尝试并没有显示工具提示中的任何变化。
除了负责工具提示的代码之外,这是我尝试过的(工具提示部分位于底部,就在 neighbor_map 对象下方):
HCP_net = Network(height='750px', width='100%', bgcolor='#222222', font_color='white')
HCP_net.barnes_hut()
sources = df2['HCP_Name']
targets = df2['HCPOffset_Name']
weights = df2['PATIENT_ID (DCOUNT)']
edge_data = zip(sources, targets, weights)
for e in edge_data:
src = e[0]
dst = e[1]
w = e[2]
HCP_net.add_node(src, src, title=src)
HCP_net.add_node(dst, dst, title=dst)
HCP_net.add_edge(src, dst, value=w)
neighbor_map = HCP_net.get_adj_list()
node_weight = HCP_net.get_edges() #my attempt at creating a weight object to call, if I call print(len(node_weight)) I get an integer so I know this is working
for node in HCP_net.nodes:
node['title'] += ' Neighbors: <br>' + '<br>'.join(neighbor_map[node['id']])
node['value'] = len(neighbor_map[node['id']])
node['value'] = len(neighbor_map.keys()) #This called by itself
(print(len(neighbor_map.keys())) also displays the same integer as my node_weight object
我想我只是不太了解如何正确调用 node['value'] 以在工具提示中产生新的显示。非常感谢任何帮助!
