简体   繁体   English

使用Graphviz绘制Networkx子图

[英]Drawing Networkx subgraph with Graphviz

Given a network like the one below: 鉴于如下网络:

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

Using the networkx I can only plot a subgraph with the connections of the Ronaldo node with the following code: 使用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

How can I draw the same subgraph, only with Ronaldo's connections, using Graphviz? 我怎样才能使用Graphviz绘制相同的子图,只有罗纳尔多的联系?

To plot using graphviz, simply point the nodes and edges of the subroutine with networkx 要使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM