Networkx:绘制平行边 [英] Networkx: Drawing parallel edges

查看:72
本文介绍了Networkx:绘制平行边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码从数据框中绘制图形:

将pandas导入为pd将 networkx 导入为 nxdf = pd.DataFrame({'id_emp' : [13524791000109, 12053850000137, 4707821000113, 4707821000114, 1],'name_emp': ['Cristiano', 'Gaúcho', 'Fenômeno','Angelin', 'Souza'],'name_dep': ['罗纳尔多','罗纳尔多', '罗纳尔多', '罗纳尔多', '布鲁诺'],'weight_1': [8,9,10,11,12],'weight_2':[5,6,7,8,9] })G = nx.MultiDiGraph()G.add_nodes_from(df['id_emp'], 二分 = 0)emp = [v for v in G.nodes if G.nodes[v]['bipartite'] == 0]G.add_nodes_from(df['name_dep'], 二分 = 1)dep = [v for v in G.nodes if G.nodes[v]['bipartite'] == 1]G.add_weighted_edges_from(df[['name_dep', 'id_emp', 'weight_1']].values)G.add_weighted_edges_from(df[['id_emp', 'name_dep', 'weight_2']].values)edge_width = [a[2]['weight']//2 for a in G.edges(data=True)]plt.figure(figsize=(5,5))pos = nx.spring_layout(G, k=0.9)nx.draw_networkx_nodes(G, pos, nodelist=dep, node_color='#bfbf7f', node_shape="h", node_size=300, with_labels = True)nx.draw_networkx_nodes(G, pos, nodelist=emp, node_color='red', node_size=300, with_labels = True)nx.draw_networkx_edges(G, pos, width=edge_width, alpha=0.2)plt.axis('关闭')plt.show()

输出:

在显示的示例中,每个顶点都有一个输入边和一个输出边,这将配置两个顶点之间的平行边.然而,networkx 绘制的图的边彼此重叠,给人的印象是两个顶点之间只有一条边.那么,如何配置 networkx 才能使输出类似于下图?

解决方案

您也可以使用

Plot a graph from a dataframe with the code below:

import pandas as pd
import networkx as nx

df = pd.DataFrame({'id_emp' : [13524791000109, 12053850000137, 4707821000113, 4707821000114, 1],
           'name_emp': ['Cristiano', 'Gaúcho', 'Fenômeno','Angelin', 'Souza'],
           'name_dep': ['Ronaldo','Ronaldo', 'Ronaldo', 'Ronaldo', 'Bruno'],
           'weight_1': [8,9,10,11,12],
           'weight_2':[5,6,7,8,9] })

 G = nx.MultiDiGraph()

 G.add_nodes_from(df['id_emp'], bipartite = 0)
 emp = [v for v in G.nodes if G.nodes[v]['bipartite'] == 0]

 G.add_nodes_from(df['name_dep'], bipartite = 1)
 dep = [v for v in G.nodes if G.nodes[v]['bipartite'] == 1]

 G.add_weighted_edges_from(df[['name_dep', 'id_emp', 'weight_1']].values)
 G.add_weighted_edges_from(df[['id_emp', 'name_dep', 'weight_2']].values)
 edge_width = [a[2]['weight']//2 for a in G.edges(data=True)]

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

 pos = nx.spring_layout(G, k=0.9)
 nx.draw_networkx_nodes(G, pos, nodelist=dep, node_color='#bfbf7f', node_shape="h", node_size=300, with_labels = True)
 nx.draw_networkx_nodes(G, pos, nodelist=emp, node_color='red', node_size=300, with_labels = True)
 nx.draw_networkx_edges(G, pos, width=edge_width, alpha=0.2)

 plt.axis('off')
 plt.show()

output:

In the example shown, each vertex has an input edge and an output edge, which would configure parallel edges between two vertices. However the networkx plots the graph with the edges overlapping each other, giving the impression that between two vertices there is only one edge. So, how can I configure the networkx so that the output is similar to the image below?

解决方案

You could also use graphviz python library.

sudo apt-get install graphviz
pip install graphviz

I tried on Jupyter notebook (which is natively supported)

import pandas as pd
import networkx as nx

df = pd.DataFrame({'id_emp' : [13524791000109, 12053850000137, 4707821000113, 4707821000114, 1],
           'name_emp': ['Cristiano', 'Gaúcho', 'Fenômeno','Angelin', 'Souza'],
           'name_dep': ['Ronaldo','Ronaldo', 'Ronaldo', 'Ronaldo', 'Bruno'],
           'weight_1': [8,9,10,11,12],
           'weight_2':[5,6,7,8,9] })

G = nx.MultiDiGraph()

G.add_nodes_from(df['id_emp'], bipartite = 0)
emp = [v for v in G.nodes if G.nodes[v]['bipartite'] == 0]

G.add_nodes_from(df['name_dep'], bipartite = 1)
dep = [v for v in G.nodes if G.nodes[v]['bipartite'] == 1]

G.add_weighted_edges_from(df[['name_dep', 'id_emp', 'weight_1']].values)
G.add_weighted_edges_from(df[['id_emp', 'name_dep', 'weight_2']].values)
edge_width = [a[2]['weight']//2 for a in G.edges(data=True)]

########################################################################
###########################CODE TO ADD##################################
########################################################################
import graphviz

d = graphviz.Digraph()

for n in dep:
    d.node(str(n), color="#bfbf7f")

for n in emp:
    d.node(str(n), color="red")

for e in G.edges:
    d.edge(str(e[0]), str(e[1]))

d.attr(size='8')

# To display the graph on Jupyter
d

Displays :

这篇关于Networkx:绘制平行边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆