網站首頁 編程語言 正文
NetworkX是一個非常強大的網絡科學工具,它封裝了圖的數據結構和許多經典圖算法,也內置了許多可視化函數可供調用。
1. 隨機圖生成
最經典的隨機圖當屬我們在上一篇博客《Erdos-Renyi隨機圖的生成方式及其特性》中講到的Erd?s-Rény隨機圖了,我們這里選用其中的Gnp?np形式,調用以下API:
G = nx.erdos_renyi_graph(10, 0.3, seed=1)
這里表示生成10個頂點的圖,且圖的每條邊都以0.3的概率產生。
當然,此時生成的圖不具有權重,我們想在此基礎上均勻隨機初始化[0, 0.4]之間的權重,可以這樣寫:
G = nx.Graph() for u, v in nx.erdos_renyi_graph(10, 0.3, seed=1).edges(): G.add_edge(u, v, weight=random.uniform(0, 0.4))
2. 2D布局可視化
隨機圖生成好之后,我們就要對其進行可視化了。首先我們需要計算每個節點在圖中擺放的位置,經典的Fruchterman-Reingold force-directed 算法可以完成這個操作,對應NetworkX中的spring_layout
函數:
pos = nx.spring_layout(G, iterations=20) #我們設算法迭代次數為20次
然后就可以分別繪制圖的邊、節點和節點標簽了:
nx.draw_networkx_edges(G, pos, edge_color="orange") nx.draw_networkx_nodes(G, pos, node_color="black") nx.draw_networkx_labels(G, pos, font_color="white") plt.show()
繪圖結果如下:
當然,這樣圖的權值是無法體現于圖上的,如果我們需要圖的權值體現于圖上,可以使圖中邊的寬度按照權值大小來設置:
nx.draw_networkx_edges(G,pos, width=[float(d['weight']*10) for (u,v,d) in G.edges(data=True)], edge_color="orange") nx.draw_networkx_nodes(G,pos, node_color="black") nx.draw_networkx_labels(G, pos, font_color="white") plt.show()
此時的繪圖結果如下:
3. 3D布局可視化
如果你覺得2D布局過于扁平,還不夠直觀地體現節點之間的拓撲關系,那你可以采用如下的代碼對圖進行三維可視化:
# 3d spring layout pos = nx.spring_layout(G, dim=3, seed=779) # Extract node and edge positions from the layout node_xyz = np.array([pos[v] for v in sorted(G)]) edge_xyz = np.array([(pos[u], pos[v]) for u, v in G.edges()]) # Create the 3D figure fig = plt.figure() ax = fig.add_subplot(111, projection="3d") # Plot the nodes - alpha is scaled by "depth" automatically ax.scatter(*node_xyz.T, s=100, ec="w") # Plot the edges for vizedge in edge_xyz: ax.plot(*vizedge.T, color="tab:gray") def _format_axes(ax): """Visualization options for the 3D axes.""" # Turn gridlines off ax.grid(False) # Suppress tick labels for dim in (ax.xaxis, ax.yaxis, ax.zaxis): dim.set_ticks([]) # Set axes labels ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") _format_axes(ax) fig.tight_layout() plt.show()
此時的繪圖結果如下:
參考
- [1]?https://networkx.org/documentation/stable/reference/
原文鏈接:https://www.cnblogs.com/orion-orion/p/16256657.html
相關推薦
- 2022-10-12 docker配置阿里云鏡像倉庫的實現_docker
- 2024-03-15 docker安裝RabbitMq插件
- 2022-03-25 C++實現希爾排序算法實例_C 語言
- 2022-08-23 Python可視化模塊altair的使用詳解_python
- 2022-10-03 pytest文檔內置fixture的request詳情_python
- 2022-11-01 Flask?sqlalchemy一對多與多對一與一對一及多對多關系介紹_python
- 2022-06-10 Flutter實現心動的動畫特效_Android
- 2022-06-06 一文搞懂Redis中String數據類型_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支