提供如下所示的网络:
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','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)]
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
使用networkx,我只能使用以下代码绘制带有Ronaldo
节点连接的子图:
dep = "Ronaldo"
lista_subset_graph = list(df[df["name_dep"] == dep]["id_emp"]) + [dep]
H = G.subgraph(lista_subset_graph)
nx.draw(H, style = "solid", with_labels = True)
plt.figure(figsize=(10,10))
plt.show() # display
如何仅使用Ronaldo的连接使用Graphviz绘制同一子图?
答案 0 :(得分:0)
要使用graphviz进行绘制,只需使用networkx指向子例程的节点和边缘
dep = "Ronaldo"
lista_subset_graph = list(df[df["name_dep"] == dep]["id_emp"]) + [dep]
H = G.subgraph(lista_subset_graph)
dr = graphviz.Digraph(format='png')
for n in H.nodes():
dr.node(str(n))
for n in H.edges():
dr.edge(str(n[0]), str(n[1]))
dr