将单纯形从 Delaunay 三角剖分转换为 networkx 图

Convert simplices from Delaunay Triangulation to networkx graph

这是 post 的后续。

我正在尝试将从 Scipy 的 Delaunay 三角剖分返回的单纯形转换为 Networkx 图。

代码:

from scipy.spatial import Delaunay as scipy_Delaunay
# tri = scipy_Delaunay(pts[:, 0:2]) #input points
# simplices = tri.simplices
   
simplices = np.array([[ 9, 13, 19],
                     [11,  9,  4],
                     [ 9, 11, 13],
                     [ 0,  7,  2],
                     [ 7,  3, 18]])
G = nx.Graph(simplices)
for path in simplices:
    nx.add_path(G, path)

nx.draw(G, with_labels=True, node_size=500, node_color='lightgreen')

错误:

raise nx.NetworkXError(f"Adjacency matrix not square: nx,ny={A.shape}")
networkx.exception.NetworkXError: Adjacency matrix not square: nx,ny=(5, 3)
networkx.exception.NetworkXError: Input is not a correct numpy matrix or array.

我不知道如何解决这个错误。建议将非常有帮助。

我认为你可以从

中移除单纯形
G = nx.Graph(simplices)

至:

G = nx.Graph()

创建一个空图表。您稍后将在循环中添加节点,因此无需在图形创建期间添加节点位置。最终代码为:

from scipy.spatial import Delaunay as scipy_Delaunay
# tri = scipy_Delaunay(pts[:, 0:2]) #input points
# simplices = tri.simplices
   
simplices = np.array([[ 9, 13, 19],
                     [11,  9,  4],
                     [ 9, 11, 13],
                     [ 0,  7,  2],
                     [ 7,  3, 18]])
G = nx.Graph()
for path in simplices:
    nx.add_path(G, path)

nx.draw(G, with_labels=True, node_size=500, node_color='lightgreen')