【问题标题】:Convert simplices from Delaunay Triangulation to networkx graph将单纯形从 Delaunay 三角剖分转换为 networkx 图
【发布时间】:2021-12-19 08:38:05
【问题描述】:

这是帖子here的后续内容。

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

我不确定如何解决此错误。建议会很有帮助。

【问题讨论】:

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


    【解决方案1】:

    我认为你可以删除单纯形

    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')
    

    【讨论】:

      猜你喜欢
      • 2020-10-17
      • 2016-05-19
      • 2020-10-20
      • 2019-06-22
      • 2016-02-08
      • 2014-01-21
      • 2019-02-16
      • 2012-03-04
      • 2015-01-07
      相关资源
      最近更新 更多