gpt4 book ai didi

python - 将无向 NetworkX 图转换为有向 NetworkX 图

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:56 30 4
gpt4 key购买 nike

我在使用以下代码时遇到问题。边连接节点。但是是否有可能有一个定向网络,如果一个“人”跟随一个“人”,但它只是一种方式,在边缘有箭头或方向。

plt.figure(figsize=(12, 12))

#Create the graph
g = nx.from_pandas_dataframe(peoples_only, source='people_id', target='mans_id')

layout = nx.spring_layout(g,k=0.05, iterations=1)

people_size = [g.degree(people) * 30 for people in peoples]
nx.draw_networkx_nodes(g,
layout,
nodelist=peoples,
node_size=people_size, # a LIST of sizes, based on g.degree
node_color='lightblue')

#draw all
nx.draw_networkx_nodes(g, layout, nodelist=mans, node_color='#cccccc', node_size=100)

#draw popular peoples
popular_mans = [man for man in mans if g.degree(man) > 1]
nx.draw_networkx_nodes(g, layout, nodelist=popular_mans, node_color='orange', node_size=100)

nx.draw_networkx_edges(g, layout, width=1, edge_color="#cccccc")

node_labels = dict(zip(peoples, peoples))
nx.draw_networkx_labels(g, layout, labels=None)

nx.write_gexf(g, "test.gexf")

plt.axis('off')
plt.title("People mans network")
plt.show()

最佳答案

创建图形时指定create_using=nx.DiGraph()

g = nx.from_pandas_edgelist(peoples_only, source='people_id', target='mans_id', create_using=nx.DiGraph()) 

这将为您提供从 people_id 到 mans_id 的有向图。

关于python - 将无向 NetworkX 图转换为有向 NetworkX 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53023605/

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