使用 networkx 绘制多部分图

Drawing a multipartite graph using networkx

我有这个图结构

graph = thisdict['data']['graph']

dict_edges = graph['edges']

edges = []

for edge in dict_edges:
    edges.append((edge['source']['node_id'], edge['target']['node_id']))

print('Source:\t\t\t     Target:\n')
    
for edge in edges:
    print(str(edge))

print('\n')

dict_nodes = graph['nodes']

nodes = {}

for node in dict_nodes:
    nodes[node['id']] = node['name']

print('Node ID:\t\t    Node Name:\n')

for key, value in nodes.items():
    print("'%s':'%s'" %(key, value))

输出:

Source:                                Target:

('61697b94f74c92a808641ba3', '61697b95f74c92a808641ba4')
('61697b94f74c92a808641ba3', '61697b96f74c92a808641ba5')
('61697b95f74c92a808641ba4', '61697b96f74c92a808641ba6')
('61697b96f74c92a808641ba6', '61697b97f74c92a808641ba7')
('61697b96f74c92a808641ba5', '61697b97f74c92a808641ba7')
('61697b97f74c92a808641ba7', '61697b98f74c92a808641ba8')
('61697b98f74c92a808641ba8', '61697b98f74c92a808641ba9')


Node ID:            Node Name:

'61697b94f74c92a808641ba3':'S3 connector'
'61697b95f74c92a808641ba4':'loader 1'
'61697b96f74c92a808641ba5':'loader 2'   
'61697b96f74c92a808641ba6':'sampler 1'
'61697b97f74c92a808641ba7':'concator'
'61697b98f74c92a808641ba8':'sampler 2'
'61697b98f74c92a808641ba9':'splitter'

我写了这段代码来画图:

nx_graph = nx.Graph()
plt.figure(figsize=(3,3))

for key, value in nodes.items():
    nx_graph.add_node(key, layer = nodes.values())
    #I need to put every node name in a single layer, So I should have 6 layers

for edge in edges:
    nx_graph.add_edge(*edge)

pos = nx.multipartite_layout(nx_graph, subset_key="layer")

nx.draw(nx_graph, pos, labels=nodes, with_labels=True)

plt.show()

它显示错误说:TypeError: 不支持的操作数类型 -: 'dict_values' 和 'float'

我需要将每个节点名称放在一个层中,所以我应该有 6 个层。 图层排序应如下所示:

S3 connector --> loader 1 & loader 2
                 loader 1 will give sampler 1
                 loader 2 & sampler 1 will meet in concator
                 concator will give sampler 2
                 sampler 2 will give splitter

发生错误是因为当您在 for 循环中添加节点时,您传递的是 layer = nodes.values() 而不是 layer=value。但是,更正它仍然不会为您提供所需的布局,因为您实际上必须以某种方式指定图层。根据您对 6 层的描述,我将它们添加为单独字典中的属性。

我通过更改为 DiGraph 并确定 sampler 1 点头的位置,对您实际尝试做的事情做出了一些假设。

这是一个独立的代码块及其输出。

import networkx as nx
import matplotlib.pyplot as plt


nodes = {'61697b94f74c92a808641ba3':'S3 connector',
'61697b95f74c92a808641ba4':'loader 1',
'61697b96f74c92a808641ba5':'loader 2',
'61697b96f74c92a808641ba6':'sampler 1',
'61697b97f74c92a808641ba7':'concator',
'61697b98f74c92a808641ba8':'sampler 2',
'61697b98f74c92a808641ba9':'splitter'}

edges = [('61697b94f74c92a808641ba3', '61697b95f74c92a808641ba4'),
('61697b94f74c92a808641ba3', '61697b96f74c92a808641ba5'),
('61697b95f74c92a808641ba4', '61697b96f74c92a808641ba6'),
('61697b96f74c92a808641ba6', '61697b97f74c92a808641ba7'),
('61697b96f74c92a808641ba5', '61697b97f74c92a808641ba7'),
('61697b97f74c92a808641ba7', '61697b98f74c92a808641ba8'),
('61697b98f74c92a808641ba8', '61697b98f74c92a808641ba9')]

layers = {'61697b94f74c92a808641ba3': 1,
'61697b95f74c92a808641ba4': 2,
'61697b96f74c92a808641ba5':2,
'61697b96f74c92a808641ba6':3,
'61697b97f74c92a808641ba7':4,
'61697b98f74c92a808641ba8':5,
'61697b98f74c92a808641ba9':6}

nx_graph = nx.DiGraph() # Made this a DiGraph (adds arrows to visual)
plt.figure(figsize=(8,8)) # Enlarged figure

for key, value in nodes.items():
    nx_graph.add_node(key, name=value, layer=layers[key])

for edge in edges:
    nx_graph.add_edge(*edge)

pos = nx.multipartite_layout(nx_graph, subset_key="layer")

nx.draw(nx_graph, pos=pos, labels=nodes, with_labels=True)

plt.show()