0

这是此处帖子的后续内容。

我正在尝试将从 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.

我不确定如何解决此错误。建议将非常有帮助。

4

1 回答 1

0

我认为您可以从中删除单纯形

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')
于 2021-11-05T09:27:30.940 回答