3

所以我有来自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()
4

2 回答 2

5

您可以使用 dict 理解,例如

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()

在此处输入图像描述

于 2012-12-09T21:50:05.197 回答
1

NetworkX Random Geometric Graph Implementation using KD 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()

在此处输入图像描述

于 2012-12-14T03:34:36.777 回答