python—networkx:畫隨機幾何圖,找出中心節點並按路徑長度染色

來源:互聯網
上載者:User

標籤:

      • 隨機產生一個集合圖,內建路徑屬性,圖大小為1*1,找出距離中心[0.5, 0.5]最近的節點,並按照路徑染色。
        NetworkX Examples ? Drawing ? Random Geometric Graph

        <pre name="code" class="python">#coding:utf-8import networkx as nximport matplotlib.pyplot as pltG=nx.random_geometric_graph(200,0.125)# position is stored as node attribute data for random_geometric_graphpos=nx.get_node_attributes(G,'pos')# find node near center (0.5,0.5)找到中心節點並求最近的節點,設為ncenterdmin=1ncenter=0for 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;">按距離中心點的路徑長度染色</span>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,pos,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()


著作權聲明:歡迎轉載,轉載請註明出處http://blog.csdn.net/ztf312/

python—networkx:畫隨機幾何圖,找出中心節點並按路徑長度染色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.