我有一个数据帧,其中包含source:person 1、target:person 2和in_rewards_program:binary。
我使用pyvis包创建了一个网络。“
got_net = Network(notebook=True, height="750px", width="100%")
# got_net = Network(notebook=True, height="750px", width="100%", bgcolor="#222222", font_color="white")
# set the physics layout of the network
got_net.barnes_hut()
got_data = df
sources = got_data['source']
targets = got_data['target']
# create graph using pviz network
edge_data = zip(sources, targets)
for e in edge_data:
src = e[0]
dst = e[1]
#add nodes and edges to the graph
got_net.add_node(src, src, title=src)
got_net.add_node(dst, dst, title=dst)
got_net.add_edge(src, dst)
neighbor_map = got_net.get_adj_list()
# add neighbor data to node hover data
for node in got_net.nodes:
node["title"] += " Neighbors:<br>" + "<br>".join(neighbor_map[node["id"]])
node["value"] = len(neighbor_map[node["id"]]) # this value attrribute for the node affects node size
got_net.show("test.html")我想添加一个功能,其中节点根据in_rewards_program中的值有不同的颜色。如果源节点的值为0,则将该节点设置为红色,如果源节点的值为1,则将其设置为蓝色。我不知道该怎么做。
发布于 2020-06-11 02:10:42
没有多少信息可以更多地了解您的数据,但根据您的代码,我可以假设您可以使用"in_rewards_program“列压缩”源“和”目标“列,并在添加节点之前创建一个条件语句,以便它将根据奖励值更改节点颜色。根据pyvis documentation,您可以使用add_node方法传递color参数:
got_net = Network(notebook=True, height="750px", width="100%")
# set the physics layout of the network
got_net.barnes_hut()
sources = df['source']
targets = df['target']
rewards = df['in_rewards_program']
# create graph using pviz network
edge_data = zip(sources, targets, rewards)
for src, dst, reward in edge_data:
#add nodes and edges to the graph
if reward == 0:
got_net.add_node(src, src, title=src, color='red')
if reward == 1:
got_net.add_node(dst, dst, title=dst, color='blue')
got_net.add_edge(src, dst)https://stackoverflow.com/questions/62202944
复制相似问题