2012-12-09 69 views
3

所以我必須從networkx例子的代碼,但我想,以圖內的隨機幾何曲線,以弄清楚如何半徑「R」內限制節點一圈的界限。我知道我會怎麼做邏輯明智的,但我是一個有點困惑如何一切正常,並已試圖與無解算出它自己出去爲止。謝謝您的幫助!半徑r內Networkx隨機幾何圖形限制節點

import networkx as nx 
import matplotlib.pyplot as plt 

G = nx.random_geometric_graph(1000,0.1) 

# position is stored as node attribute data for random_geometric_graph 
pos = nx.get_node_attributes(G,'pos') 

# find node near center (0.5,0.5) 
dmin =1 
ncenter =0 
for n in pos: 
    x,y = pos[n] 
    d = (x-0.5)**2+(y-0.5)**2 
    if d<dmin: 
     ncenter = n 
     dmin = d 

# color by path length from node near center 
p = nx.single_source_shortest_path_length(G,ncenter) 

plt.figure(figsize=(8,8)) 
#node_color=p.values() 
nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4) 
nx.draw_networkx_nodes(G,pos,nodelist=p.keys(), 
        node_size=80, 
        node_color='#0F1C95', 
        cmap=plt.cm.Reds_r) 

plt.xlim(-0.05,1.05) 
plt.ylim(-0.05,1.05) 
plt.axis('off') 
plt.savefig('random_geometric_graph.png') 
plt.show() 

回答

5

你可以使用字典理解諸如

p = {node:length for node, length in nx.single_source_shortest_path_length(G,ncenter).items() 
    if length < 5} 

的字典限制這些節點從ncenter的距離爲< 5.

對於python2.6的或以上,你可以使用

p = dict((node, length) for node, length in nx.single_source_shortest_path_length(G,ncenter).items() 
    if length < 5) 

你也可以用一行代碼替換

dmin =1 
ncenter =0 
for n in pos: 
    x,y = pos[n] 
    d = (x-0.5)**2+(y-0.5)**2 
    if d<dmin: 
     ncenter = n 
     dmin = d 

ncenter, _ = min(pos.items(), key = lambda (node, (x,y)): (x-0.5)**2+(y-0.5)**2) 

僅繪製其距離ncenter是< 5這些節點,定義子圖:

H = G.subgraph(p.keys())  
nx.draw_networkx_edges(H, pos, alpha = 0.4) 
nx.draw_networkx_nodes(H, pos, node_size = 80, node_color = node_color, 
         cmap = plt.get_cmap('Reds_r')) 

import networkx as nx 
import matplotlib.pyplot as plt 
G = nx.random_geometric_graph(1000, 0.1) 

# position is stored as node attribute data for random_geometric_graph 
pos = nx.get_node_attributes(G, 'pos') 

# find node near center (0.5,0.5) 
ncenter, _ = min(pos.items(), key = lambda (node, (x, y)): (x-0.5)**2+(y-0.5)**2) 

# color by path length from node near center 
p = {node:length 
    for node, length in nx.single_source_shortest_path_length(G, ncenter).items() 
    if length < 5} 

plt.figure(figsize = (8, 8)) 
node_color = p.values() 
H = G.subgraph(p.keys())  
nx.draw_networkx_edges(H, pos, alpha = 0.4) 
nx.draw_networkx_nodes(H, pos, node_size = 80, node_color = node_color, 
         cmap = plt.get_cmap('Reds_r')) 

plt.xlim(-0.05, 1.05) 
plt.ylim(-0.05, 1.05) 
plt.axis('off') 
plt.savefig('random_geometric_graph.png') 
plt.show() 

enter image description here

+0

謝謝!這非常有幫助。如果你只想在半徑內繪製邊緣怎麼辦?重寫原始的random_geometric_graph函數會更有效率嗎? – marth

1

用於NetworkX Random Geometric Graph Implementation using K-D Trees 問題的答案可以被用於有效地執行此,例如

import numpy as np 
from scipy import spatial 
import networkx as nx 
import matplotlib.pyplot as plt 
n = 100 
radius = 0.4 
# random sample n points in disc using rejection 
positions = np.random.uniform(low=-radius, high=radius, size=(n*2.0,2)) 
disc = np.array([p for p in positions if np.linalg.norm(p) < radius][0:n]) 
# kdtree data structure of points in disc 
kdtree = spatial.KDTree(disc) 
# make graph 
G = nx.Graph() 
G.add_nodes_from(range(n)) 
r = 0.1 # connect nodes if distance < r 
pairs = kdtree.query_pairs(r) 
G.add_edges_from(list(pairs)) 
# draw 
pos = dict(zip(range(n),disc)) 
nx.draw(G,pos,with_labels=False,node_size=25) 
circ=plt.Circle((0,0),radius=radius,alpha=0.1) 
ax=plt.gca() 
plt.axis('equal') 
ax.add_patch(circ) 
plt.savefig('disc.png') 
plt.show() 

enter image description here