簡體   English   中英

如何從已經使用networkx創建的多個節點添加多個邊?

[英]How to add multiple edges from multiple nodes that are already created with networkx?

當我創建一個帶有坐標元組的節點字典時,如何將一個節點的邊添加到另一個節點並保持圖形穩定而不移動任何東西?

我也在尋找有關 networkx 和谷歌搜索文檔的其他解決方案。 我發現是使用函數add_edges_from()函數,它創建從節點到節點的路徑。 但是,這樣做時,這將不在正確的坐標中,並且基本上會四處移動。 我在 StackOverflow ( 這里) 發了一個帖子來使用節點的坐標並繪制圖形。 這就是我想要的,但現在我的權衡是我正在失去優勢。 在我的ex.txt ,我正在解析我的節點及其坐標。 解析我的節點和坐標后,我正在查看從哪個節點到另一個節點的邊緣。

ex.txt 文件:

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

蟒蛇文件:

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 :)。 提前致謝!

更新答案
假設ex.txt的內容是:

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

因此,在對您的代碼進行一些更改后,這是最終結果。 我在代碼中添加了注釋,以幫助您了解更改的內容

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'))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM