【问题标题】:Creating networkx graph using Delaunay Triangulation使用 Delaunay 三角剖分创建 networkx 图
【发布时间】:2020-10-20 16:54:14
【问题描述】:

我有一个 Delaunay 三角剖分 (DT) (scipy),如下所示:

# Take first 50 rows with 3 attributes to create a DT-
d = data.loc[:50, ['aid', 'x', 'y']].values
dt = Delaunay(points = d) 

# List of triangles in the DT-
dt.simplices                                       
'''
array([[1, 3, 4, 0],
       [1, 2, 3, 0],
       [1, 2, 3, 4]], dtype=int32)
'''

现在,我想使用“networkx”包创建一个图,并添加使用 DT 从上面找到的节点和边。

# Create an empty graph with no nodes and no edges.
G = nx.Graph()

我想出的将 DT 单纯形中的唯一节点添加到“G”中的代码是-

# Python3 list to contain nodes
nodes = []

for simplex in data_time_delaunay[1].simplices.tolist():
    for nde in simplex:
        if nde in nodes:
            continue
        else:
            nodes.append(nde)

nodes
# [1, 3, 4, 0, 2]

# Add nodes to graph-
G.add_nodes_from(nodes)

如何使用“dt.simplices”向“G”添加边?例如,第一个三角形是 [1, 3, 4, 0] 并且位于节点/顶点 1、3、4 和 0 之间。如何确定哪些节点相互连接,然后将它们作为边添加到'G'?

另外,有没有更好的方法将节点添加到“G”?

我正在使用 Python 3.8。

谢谢!

【问题讨论】:

    标签: python-3.x graph networkx delaunay scipy-spatial


    【解决方案1】:

    您可以将数组中的行添加为paths。一条路径仅构成一系列边,因此路径1,2,3 转换为边列表(1,2),(2,3)。 所以遍历行并使用nx.add_path:

    simplices = np.array([[1, 3, 4, 0],
                          [1, 2, 3, 0],
                          [1, 2, 3, 4]])
    
    G = nx.Graph()
    for path in simplices:
        nx.add_path(G, path)
    
    nx.draw(G, with_labels=True, node_size=500, node_color='lightgreen')
    

    【讨论】:

      猜你喜欢
      • 2015-01-07
      • 2019-04-18
      • 2021-12-19
      • 2010-12-14
      • 2021-05-14
      • 2013-05-12
      • 1970-01-01
      • 2016-02-08
      • 2014-01-21
      相关资源
      最近更新 更多