Python:规则网络的边长分布
Python: edge length distribution of a regular network
我正在使用 NxN
常规网络,我想绘制它的 边长分布 。
这是我生成网络的方式:
import networkx as nx
import matplotlib.pyplot as plt
N=30 #This can be changed
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
inds=labels.keys()
vals=labels.values()
inds.sort()
vals.sort()
pos2=dict(zip(vals,inds))
nx.draw_networkx(G, pos=pos2, with_labels=False, node_size = 15)
这是我计算边长分布的方法:
def plot_edge_length_distribution(): #Euclidean distances from all nodes
lengths={}
for node in G.nodes():
neigh=nx.all_neighbors(G,node) #The connected neighbors of node n
for n in neigh:
lengths[node]=((pos2[n][1]-pos2[node][1])**2)+((pos2[n][0]-pos2[node][0])**2) #The square distance
items=sorted(lengths.items())
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([k for (k,v) in items],[v/(num_edges) for (k,v) in items],'ks-')
ax.set_xscale("linear")
ax.set_yscale("linear")
plt.yticks(numpy.arange(0.94, 1.00, 0.02))
title_string=('Edge Length Distribution')
subtitle_string=('Lattice Network | '+str(N)+'x'+str(N)+' nodes')
plt.suptitle(title_string, y=0.99, fontsize=17)
plt.title(subtitle_string, fontsize=9)
plt.xlabel('Edge Length L')
plt.ylabel('p(L)')
ax.grid(True,which="both")
plt.show()
plot_edge_length_distribution()
这是我得到的:由于规则网格的性质,字典 lengths
应该只包含一个作为值。
这就是我想要的:一个图告诉我长度=1 的概率为 p(l)=1,因为规则网格仅具有长度为 1 的边。我的代码有什么问题?
迭代边缘并计算每一个边缘的距离更容易和更快:
In [1]: import networkx as nx
In [2]: from math import sqrt
In [3]: from collections import Counter
In [4]: G = nx.grid_2d_graph(100,100)
In [5]: d = Counter(sqrt((x-a)**2 + (y-b)**2) for (x,y),(a,b) in G.edges())
In [6]: print(d)
Counter({1.0: 19800})
我正在使用 NxN
常规网络,我想绘制它的 边长分布 。
这是我生成网络的方式:
import networkx as nx
import matplotlib.pyplot as plt
N=30 #This can be changed
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
inds=labels.keys()
vals=labels.values()
inds.sort()
vals.sort()
pos2=dict(zip(vals,inds))
nx.draw_networkx(G, pos=pos2, with_labels=False, node_size = 15)
这是我计算边长分布的方法:
def plot_edge_length_distribution(): #Euclidean distances from all nodes
lengths={}
for node in G.nodes():
neigh=nx.all_neighbors(G,node) #The connected neighbors of node n
for n in neigh:
lengths[node]=((pos2[n][1]-pos2[node][1])**2)+((pos2[n][0]-pos2[node][0])**2) #The square distance
items=sorted(lengths.items())
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([k for (k,v) in items],[v/(num_edges) for (k,v) in items],'ks-')
ax.set_xscale("linear")
ax.set_yscale("linear")
plt.yticks(numpy.arange(0.94, 1.00, 0.02))
title_string=('Edge Length Distribution')
subtitle_string=('Lattice Network | '+str(N)+'x'+str(N)+' nodes')
plt.suptitle(title_string, y=0.99, fontsize=17)
plt.title(subtitle_string, fontsize=9)
plt.xlabel('Edge Length L')
plt.ylabel('p(L)')
ax.grid(True,which="both")
plt.show()
plot_edge_length_distribution()
这是我得到的:由于规则网格的性质,字典 lengths
应该只包含一个作为值。
这就是我想要的:一个图告诉我长度=1 的概率为 p(l)=1,因为规则网格仅具有长度为 1 的边。我的代码有什么问题?
迭代边缘并计算每一个边缘的距离更容易和更快:
In [1]: import networkx as nx
In [2]: from math import sqrt
In [3]: from collections import Counter
In [4]: G = nx.grid_2d_graph(100,100)
In [5]: d = Counter(sqrt((x-a)**2 + (y-b)**2) for (x,y),(a,b) in G.edges())
In [6]: print(d)
Counter({1.0: 19800})