首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从已经用networkx创建的多个节点中添加多个边?

如何从已经用networkx创建的多个节点中添加多个边?
EN

Stack Overflow用户
提问于 2019-04-05 00:41:09
回答 1查看 574关注 0票数 2

当我创建一个具有坐标元组的节点字典时,如何将节点的边添加到节点,并保持图的稳定而不需要任何移动?

我一直在寻找其他解决方案,包括networkx文档和谷歌搜索。我发现的是使用一个函数add_edges_from()函数,它创建一个从节点到节点的路径。然而,当这样做时,这将不是在正确的坐标下,基本上是在移动。我在StackOverflow (here)中发布了一个帖子,以使用节点的坐标并绘制图形。这是我想要的,但现在我的权衡是我正在失去我的优势。在我的ex.txt中,我解析我的节点和它的坐标。在解析完我的节点和坐标之后,我将从哪个节点寻找一个边缘到另一个节点。

ex.txt文件:

代码语言:javascript
复制
3
a2a 5 0
##start
a0 1 2
##end
a1 9 2
3 5 4
a0-a2a
a0-3
a2a-1
3-1
a2a-3

python文件:

代码语言:javascript
复制
import re
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
def file_parsing(file_path):
    cnt = 0
    output_list = []

    with open(file_path, 'r') as fp:
        for line in fp:
            cnt += 1
            #checks for the room name and coordinates
            if re.match('([^\s#]{1,10}) (\d+) (\d+)', line, re.MULTILINE):
                output_list.append(line.strip().split(' '))
            #checks for start
            if line.startswith('##start'):
                output_list.append(next(fp, '').strip().split())
            #checks for start
            if line.startswith('##end'):
                output_list.append(next(fp, '').strip().split())
    room_name = [item[0] for item in output_list]
    x_coord = [int(item[1]) for item in output_list]
    y_coord = [int(item[2]) for item in output_list]
    x_y = list(zip(x_coord, y_coord))
    pos_dict = dict(zip(room_name, x_y))
    return pos_dict

room_pos_dict = file_parsing('ex.txt')
print(room_pos_dict)
G = nx.Graph()
G.add_nodes_from(room_pos_dict.keys())
nx.set_node_attributes(G, room_pos_dict, 'pos')
# nx.set_edge_attributes(G, room_pos_dict.values(), 'pos')
nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'pos'))

这将是我绘制的理想图表:

但是现在,它们被随机地放置在任何地方。

现在,从我上一篇文章中(谢谢Mohammed Kashif),我得到了我所有节点的位置,但不是边缘:

我所期望的是两个节点的组合:节点及其位置,以及连接到每个节点的边缘。

请原谅我,我正在努力学习Python和networkx :)。提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-05 09:34:52

最新答案

假设ex.txt的内容是:

代码语言:javascript
复制
3
a2a 5 0
##start
a0 1 2
##end
a1 9 2
3 5 4
a0-a2a
a0-3
a2a-a1
3-a1
a2a-3

因此,在对代码进行了一些更改之后,这是最后的结果。我在代码中添加了注释,以帮助您了解已更改的内容。

代码语言:javascript
复制
import re
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline

def file_parsing(file_path):
    cnt = 0
    output_list = []
    edge_list = []
    with open(file_path, 'r') as fp:
        for line in fp:
            cnt += 1
            #checks for the room name and coordinates
            if re.match('([^\s#]{1,10}) (\d+) (\d+)', line, re.MULTILINE):
                output_list.append(line.strip().split(' '))
            #checks for start
            if line.startswith('##start'):
                output_list.append(next(fp, '').strip().split())
            #checks for start
            if line.startswith('##end'):
                output_list.append(next(fp, '').strip().split())

            # --------- Check for edges -----------#
            if '-' in line:
                src, dest = line.split('-')
                edge_list.append([src.strip(), dest.strip()])
    room_name = [item[0] for item in output_list]
    x_coord = [int(item[1]) for item in output_list]
    y_coord = [int(item[2]) for item in output_list]
    x_y = list(zip(x_coord, y_coord))
    pos_dict = dict(zip(room_name, x_y))

    return pos_dict, edge_list

room_pos_dict, edge_list = file_parsing('ex.txt')

G = nx.DiGraph()
G.add_nodes_from(room_pos_dict.keys())

#----------- Add edges from the edge list ------ 
G.add_edges_from(edge_list)

nx.set_node_attributes(G, room_pos_dict, 'pos')
nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'pos'))

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55526758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档