Networkx随机几何图形限制半径r内的节点

时间:2012-12-09 21:29:10

标签: python graph networkx

所以我从networkx示例中得到了这个代码,但是我想弄清楚如何限制半径'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)
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()

2 个答案:

答案 0 :(得分:5)

您可以使用词典理解,例如

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

将dict限制为距离ncenter的距离

对于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的距离<1的节点。 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

答案 1 :(得分: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