首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Networkx pyvis:更改节点的颜色

Networkx pyvis:更改节点的颜色
EN

Stack Overflow用户
提问于 2020-06-05 03:46:35
回答 1查看 3.3K关注 0票数 0

我有一个数据帧,其中包含source:person 1、target:person 2和in_rewards_program:binary。

我使用pyvis包创建了一个网络。“

代码语言:javascript
复制
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,则将其设置为蓝色。我不知道该怎么做。

EN

回答 1

Stack Overflow用户

发布于 2020-06-11 02:10:42

没有多少信息可以更多地了解您的数据,但根据您的代码,我可以假设您可以使用"in_rewards_program“列压缩”源“和”目标“列,并在添加节点之前创建一个条件语句,以便它将根据奖励值更改节点颜色。根据pyvis documentation,您可以使用add_node方法传递color参数:

代码语言:javascript
复制
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)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62202944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档