【问题标题】:Drawing a multipartite graph using networkx使用 networkx 绘制多部图
【发布时间】:2021-10-25 11:07:10
【问题描述】:

我有这个图结构

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: unsupported operand type(s) for -: 'dict_values' and '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

【问题讨论】:

    标签: python matplotlib machine-learning data-science networkx


    【解决方案1】:

    发生错误是因为当您在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()
    

    【讨论】:

    • 谢谢您,先生! @Frodnar
    猜你喜欢
    • 2021-07-27
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多