简体   繁体   English

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

[英]Turn undirected NetworkX graph into directed NetworkX graph

I'm having a problem with the below piece of code.我在使用以下代码时遇到问题。 The edges are connecting the nodes.边连接节点。 But is it possible to have a directed network so that if one 'people' follows one 'man', but it is only one way, to have arrow or direction on the edge.但是有没有可能有一个有向网络,如果一个“人”跟随一个“人”,但这只是一种方式,在边缘有箭头或方向。

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()

When creating the graph specify create_using=nx.DiGraph()创建图形时指定create_using=nx.DiGraph()

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

That will give you a directed graph from people_id to mans_id.这将为您提供从 people_id 到 mans_id 的有向图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM