在python中用邻接矩阵和坐标表绘制连通图。

问题描述 投票:1回答:1

我有一组点(x,y)坐标和邻接矩阵,它给出了连接的细节。我想用给定的连通性绘制物理坐标。我知道 网络x 是有用的,创建一个连接图和散点图绘制物理坐标。但我想要的是两者兼而有之。

python matplotlib coordinates networkx adjacency-matrix
1个回答
2
投票

你可以用以下方法绘制坐标和连通性 Networkx.

import networkx as nx
import numpy as np

pos = np.random.rand(10, 2) #coordinates, (x, y) for 10 nodes
connect = [tuple(np.random.random_integers(0, 9, size=(2))) for x in range(8)] #random connections

#creation of the graph
graph = nx.Graph()
#adding nodes/connections in the graph
for node in range(len(pos)):
    graph.add_node(node)
graph.add_edges_from(connect)

#plot of the nodes using the (x,y) pairs as coordinates
nx.draw(graph, [(x,y) for x,y in pos], node_size=50)

graph

© www.soinside.com 2019 - 2024. All rights reserved.