边缘的重量作为标签

时间:2019-06-13 11:56:07

标签: python networkx graphviz

给出如下图:

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

输出:enter image description here

但是,重要的是要告知边缘的重量。使用G.edges.data('weight'),我可以在图G中看到每个边缘的权重值,如何将权重的值作为graphviz中边缘的标签?

1 个答案:

答案 0 :(得分:0)

source ./create_swarm.sh

您可以使用. ./create_swarm.sh 关键字参数。

print(G.edges(data=True))
# [(13524791000109, 'Ronaldo', {'weight': 4}), (12053850000137, 'Iker', {'weight': 6}), (4707821000113, 'Adriano', {'weight': 7}), (4707821000114, 'Adriano', {'weight': 9}), (1, 'Bruno', {'weight': 14})]

输出:

label

enter image description here