我需要开发一个双向网络,其中节点来自以下数据框的'id_emp'和'name_dep'列:
import networkx as nx
import pandas as pd
df = pd.DataFrame({'id_emp':[13524791000109, 12053850000137, 4707821000113, 4707821000114],
'name_emp':['Cristiano', 'Gaúcho', 'Fenômeno','Angelin'],
'name_dep': ['Ronaldo','Ronaldo', 'Ronaldo', 'Ronaldo'],
'peso': [8,9,10,11]})
在边缘:
1-他们的体重,由“体重”列确定;
2-必须对其进行指挥;
3-在原始工作中必须证明交换关系,然后两个节点之间必须有两个平行边;
我正在尝试以下代码:
G = nx.MultiDiGraph()
G.add_nodes_from(df['id_emp'], bipartite = 0)
deputados = [v for v in G.nodes if G.nodes[v]['bipartite'] == 0]
G.add_nodes_from(df['name_dep'], bipartite = 1)
empresa = [v for v in G.nodes if G.nodes[v]['bipartite'] == 1]
G.add_weighted_edges_from(df[['id_emp', 'name_dep', 'peso']].values)
edge_width = [a[2]['weight']//2 for a in G.edges(data=True)]
deputado = "Ronaldo"
lista_subset_graph = list(df[df["name_dep"] == deputado]["id_emp"]) + [deputado]
H = G.subgraph(lista_subset_graph)
nx.draw_networkx_nodes(H, pos = nx.spring_layout(H), nodelist = deputados, node_color='#bfbf7f', node_shape="h", node_size=3000, with_labels = True)
nx.draw_networkx_nodes(H, pos = nx.spring_layout(H), nodelist = empresa, node_color='#9f9fff', node_size=3000, with_labels = True)
nx.draw_networkx_edges(H, pos = nx.spring_layout(H),width=edge_width, alpha=0.2)
#nx.draw(H, style = "solid", with_labels = True)
plt.figure(figsize=(7.5, 7.5))
plt.show()
输出:
当我注释以nx.draw_networkx_nodes
和nx.draw_networkx_edges
开头的行并取消注释以nx.draw
开头的行时,输出如下:
我想保留预览作为符合边缘规格的第一张图片。