gpt4 book ai didi

python - NetworkX:从 2D numpy 数组创建的常规图会产生不匹配

转载 作者:太空宇宙 更新时间:2023-11-03 15:10:29 24 4
gpt4 key购买 nike

假设您有一个 2D numpy 数组(栅格),如下所示:

import matplotlib.pyplot as plt
import numpy as np
N=5
np.random.seed(17) #For reproducibility
A=np.random.rand(N,N)

In[1]: A
Out[1]:
array([[ 0.294665 , 0.53058676, 0.19152079, 0.06790036, 0.78698546],
[ 0.65633352, 0.6375209 , 0.57560289, 0.03906292, 0.3578136 ],
[ 0.94568319, 0.06004468, 0.8640421 , 0.87729053, 0.05119367],
[ 0.65241862, 0.55175137, 0.59751325, 0.48352862, 0.28298816],
[ 0.29772572, 0.56150891, 0.39604744, 0.78870071, 0.41848439]])

绘制后,如下所示:

im=plt.imshow(A,origin="upper",interpolation="nearest",cmap=plt.cm.gray_r)
plt.colorbar(im)

enter image description here

假设您要创建一个 2D 图(晶格网络),其中每个栅格单元对应一个节点,每个节点的大小就是该单元的值:

import networkx as nx
#Turn the matrix into a dictionary with the format {(i,j):value}
dict_of_values={(i,j):A[i][j] for i in range(0,A.shape[0]) for j in range(0,A.shape[1])}

#Create lattice network of the same size as A
G = nx.grid_2d_graph(N,N)

#Make sure the nodes are plotted according to a regular grid
labels=dict(((i,j),i + (N-1-j)*N) for i, j in G.nodes())
nx.relabel_nodes(G,labels,False) #False=relabel the nodes in place
inds=labels.keys()
vals=labels.values()
inds=[(N-j-1,N-i-1) for i,j in inds]
#Create the dictionary of positions for the grid
grid_pos=dict(zip(vals,inds)) #Format: {node ID:(i,j)}

#Look up the value for each node in the matrix A
inverted_grid_pos=dict(zip(inds,vals)) #Format: {(i,j):node ID}
#The values in A for each node come from "dict_of_values"
values=[dict_of_values.get(node) for node in inverted_grid_pos]
exaggerate_values=[300*i for i in values]

#Plot the graph with the size based on the values
plt.figure()
nx.draw(G,pos=grid_pos,with_labels=True,node_size=exaggerate_values)

此脚本返回不匹配:节点的大小与二维数组中的值不匹配。事实上,我预计节点 2121720 是最大的,但这确实不会发生。

哪里出现不匹配?

enter image description here

最佳答案

您可以直接使用转置数组 A 来获取节点大小,而不是从无序字典中生成节点大小。

import matplotlib.pyplot as plt
import numpy as np
import networkx as nx

N=4
np.random.seed(17) #For reproducibility
A=np.random.choice([200,400,800], size=(N,N))
A[3,1] = 1500
im=plt.imshow(A,origin="upper",interpolation="nearest",cmap=plt.cm.gray_r)
plt.colorbar(im)


G = nx.grid_2d_graph(N,N)

labels=dict(((i,j),i + (N-1-j)*N) for i, j in G.nodes())
nx.relabel_nodes(G,labels,False) #False=relabel the nodes in place
inds=labels.keys()
vals=labels.values()
inds=[(N-j-1,N-i-1) for i,j in inds]

#Create the dictionary of positions for the grid
grid_pos=dict(zip(vals,inds)) #Format: {node ID:(i,j)}


#Plot the graph with the size based on the values
plt.figure()
nx.draw(G,pos=grid_pos,with_labels=True,node_size=A.T.flatten())

plt.show()

enter image description here

关于python - NetworkX:从 2D numpy 数组创建的常规图会产生不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44241306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com