這是這里帖子的后續。
我正在嘗試將從 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.
我不確定如何解決此錯誤。建議將非常有幫助。
uj5u.com熱心網友回復:
我認為你可以從
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')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349914.html
下一篇:Pythoncv2:視頻不播放
