使用Delaunay三角剖分创建网络X图 [英] Creating networkx graph using Delaunay Triangulation
本文介绍了使用Delaunay三角剖分创建网络X图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个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.implices’将边添加到‘G’?例如,第一个三角形是[1,3,4,0],并且位于节点/顶点1、3、4和0之间。如何确定哪些节点相互连接,然后将它们作为边添加到‘G’?
还有,有没有更好的方法将节点添加到"G"?
我使用的是Python3.8。
谢谢!
推荐答案
您可以将数组中的行添加为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')
这篇关于使用Delaunay三角剖分创建网络X图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文