gpt4 book ai didi

python - 进一步编辑 PyVis 工具提示中的项目?

转载 作者:行者123 更新时间:2023-12-05 05:50:15 25 4
gpt4 key购买 nike

我正在使用 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

我想我只是不太了解如何正确调用节点 ['value'] 以在工具提示中产生新的显示。非常感谢任何帮助!

最佳答案

node['title'] 变量存储悬停节点时显示的信息。要将权重添加到显示的信息,您首先需要将它们与相应的邻居相关联,然后更改 node['title'] 变量,使其包含连接的信息 neighbor重量 。您可以在下面的代码中找到有关如何执行此操作的更多详细信息:

from pyvis.network import Network
import pandas as pd

got_net = Network(height='750px', width='100%', bgcolor='#222222', font_color='white')

# set the physics layout of the network
got_net.barnes_hut()
got_data = pd.read_csv('https://www.macalester.edu/~abeverid/data/stormofswords.csv')

sources = got_data['Source']
targets = got_data['Target']
weights = got_data['Weight']

edge_data = zip(sources, targets, weights)

for e in edge_data:
src = e[0]
dst = e[1]
w = e[2]

got_net.add_node(src, src, title=src)
got_net.add_node(dst, dst, title=dst)
got_net.add_edge(src, dst, value=w)

neighbor_map = got_net.get_adj_list()
edges = got_net.get_edges()
nodes=got_net.get_nodes()

N_nodes=len(nodes)
N_edges=len(edges)


weights=[[] for i in range(N_nodes)]

#Associating weights to neighbors
for i in range(N_nodes): #Loop through nodes
for neighbor in neighbor_map[nodes[i]]: #and neighbors
for j in range(N_edges): #associate weights to the edge between node and neighbor
if (edges[j]['from']==nodes[i] and edges[j]['to']==neighbor) or \
(edges[j]['from']==neighbor and edges[j]['to']==nodes[i]):
weights[i].append(edges[j]['value'])


for node,i in zip(got_net.nodes,range(N_nodes)):

node['value']=len(neighbor_map[node['id']])
node['weight']=[str(weights[i][k]) for k in range(len(weights[i]))]
list_neighbor=list(neighbor_map[node['id']])
#Concatenating neighbors and weights
hover_str=[list_neighbor[k]+' '+ node['weight'][k] for k in range(node['value'])]
#Setting up node title for hovering
node['title']+=' Neighbors:<br>'+'<br>'.join(hover_str)

got_net.show('gameofthrones.html')

输出给出:

enter image description here

关于python - 进一步编辑 PyVis 工具提示中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70546467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com