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

时间:2018-10-27 15:44:59

标签: python pandas networkx

下面的代码有问题。边缘连接节点。但是有可能拥有一个定向网络,以便如果一个“人”跟随一个“人”,但是在边缘上放置箭头或方向只是一种方法。

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

1 个答案:

答案 0 :(得分:1)

创建图形时,请指定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的有向图。