我有一个 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。
谢谢!