您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python—networkx:画随机几何图,找出中心节点并按路径长度染色

5b51 2022/1/14 8:25:25 python 字数 5761 阅读 883 来源 www.jb51.cc/python

<ulclass=\"wy-breadcrumbs\"style=\"list-style:none;\"><listyle=\"list-style:none;display:inline-block;\">

概述

<ul class="wy-breadcrumbs" style="list-style:none;"><li style="list-style:none;display:inline-block;">
<ul class="wy-breadcrumbs" style="list-style:none;"><li style="list-style:none;display:inline-block;">
<ul class="wy-breadcrumbs" style="list-style:none;">

<code class="language-python"><pre name="code" class="python">#coding:utf-8
import networkx as nx
import matplotlib.pyplot as plt

G=nx.random_geometric_graph(200,0.125)

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

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颜色定为红色,程度<span style="font-family: Arial,Helvetica,sans-serif;">按距离中心点的路径长度染色

p=nx.single_source_shortest_path_length(G,ncenter)
plt.figure(figsize=(8,8))
nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4)
nx.draw_networkx_nodes(G,nodelist=p.keys(),node_size=80,node_color=p.values(),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()

G=nx.random_geometric_graph(200,0.125)

pos=nx.get_node_attributes(G,'pos')

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

p=nx.single_source_shortest_path_length(G,ncenter)
plt.figure(figsize=(8,8))
nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4)
nx.draw_networkx_nodes(G,nodelist=p.keys(),node_size=80,node_color=p.values(),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()

G=nx.random_geometric_graph(200,0.125)

pos=nx.get_node_attributes(G,'pos')

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

p=nx.single_source_shortest_path_length(G,ncenter)
plt.figure(figsize=(8,8))
nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4)
nx.draw_networkx_nodes(G,nodelist=p.keys(),node_size=80,node_color=p.values(),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()

总结

以上是编程之家为你收集整理的python—networkx:画随机几何图,找出中心节点并按路径长度染色全部内容,希望文章能够帮你解决python—networkx:画随机几何图,找出中心节点并按路径长度染色所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶