【发布时间】:2017-05-29 11:22:26
【问题描述】:
假设你有一个像这样的二维 numpy 数组(raster):
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)
并且说你要创建一个二维图(一个格子网络),其中每个栅格单元对应一个节点,每个节点的大小就是单元的值:
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)
此脚本返回不匹配:节点的大小与二维数组中的值不匹配。事实上,我预计节点2、12、17 和20 是最大的,但这不会发生。
不匹配出现在哪里?
【问题讨论】:
标签: python arrays numpy matplotlib networkx