所以我必須從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()
謝謝!這非常有幫助。如果你只想在半徑內繪製邊緣怎麼辦?重寫原始的random_geometric_graph函數會更有效率嗎? – marth