【发布时间】:2015-03-19 02:58:42
【问题描述】:
我有 30 个节点的图网络数据(使用邻接矩阵)。该图目前如下所示:
每个集群有 15 个节点,每个节点都连接到同一集群内的其他节点。只有两对不同集群的节点相互连接。问题是我得到的图都是浓缩的,集群中的每条边都不是清晰可见的。有没有办法可以清楚地显示集群中的每个边缘。主要是喜欢使图形更大,每个节点的边缘线清晰可见。
我使用 networkx lib 的以下命令绘制了这个。
G1=nx.from_numpy_matrix(W1)
nx.draw_networkx(G1)
其中 W1 是节点的邻接矩阵 (30x30)。
请指教。
编辑:
想要这样的东西,每个节点清晰,边缘可见且不浓缩。关键是我希望上面的聚类点只显示在该聚类附近,而对于较低的聚类点也是如此。但是在每个集群中,我希望节点有点分开,以便每个边缘都清晰可见。
编辑2:
def adjacencyMatrix2():
for x in range(N):
if (x<15):
c=N/2
else:
c=N
for y in range(x+1,c):
W1[x][y]=W1[y][x]=1
# Connecting two other nodes separately.
W1[0][16]=W1[16][0]=1
W1[1][15]=W1[15][1]=1
adjacencyMatrix2()
G1=nx.from_numpy_matrix(W1)
graph_pos=nx.spring_layout(G1,k=0.50,iterations=50)
nx.draw_networkx(G1,graph_pos)
EDIT3:
N=30
# Creating a matrix of zeros.
W=np.zeros((N,N))
# Mentioning the edges to start with. Thinking of a pair of 15 node cluster with two cluster connected by two pair of nodes.
edge=[[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[1,11],[1,12],[1,13],[1,14],[1,15],
[16,17],[16,18],[16,19],[16,20],[16,21],[16,22],[16,23],[16,24],[16,25],[16,26],[16,27],[16,28],[16,29],[16,30],
[1,16],[2,17],[2,3],[5,6],[8,9],[9,4],[18,26],[17,22],[29,21],[17,28]]
# Function for creating adjacency matrix ,populating the zeros matrix with 1 and 0-signifying edges on a node.
def adjacencyMatrix():
"""This function creates an Adjacency Matrix from a edge set.
input-> set of edges to be connected
output-> Adjacency matrix (n,n)
"""
for first,second in edge:
W[first-1,second-1]=W[second-1][first-1]=1
# Creating the adjacency matrix by calling the function.
adjacencyMatrix()
我还看到,每次运行代码时,图表的布局都会发生变化。我不希望图形布局随着代码的每次运行而改变。目前它正在这样做。
【问题讨论】:
标签: python numpy pandas matplotlib