给出如下图:
import pandas as pd
import networkx as nx
import graphviz
df = pd.DataFrame({'id_emp' : [13524791000109, 12053850000137, 4707821000113, 4707821000114, 1],
'name_emp': ['Cristiano', 'Gaúcho', 'Fenômeno','Angelin', 'Souza'],
'name_dep': ['Ronaldo','Iker', 'Adriano', 'Adriano', 'Bruno'],
'Team': ['Real','Real', 'Flamengo','Flamengo','Boa'],
'weight1': [3,5,7,8,12],
'weight2': [4,6,7,9,14]})
G = nx.Graph()
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', 'weight1']].values)
G.add_weighted_edges_from(df[['id_emp', 'name_dep', 'weight2']].values)
我正在使用Graphviz绘制它:
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.edge(str(e[1]), str(e[0]))
d.attr(size='10')
d.engine='circo'
d
但是,重要的是要告知边缘的重量。使用G.edges.data('weight')
,我可以在图G中看到每个边缘的权重值,如何将权重的值作为graphviz中边缘的标签?
答案 0 :(得分:0)