python?NetworkX庫生成并繪制帶權(quán)無向圖
NetworkX是一個(gè)非常強(qiáng)大的網(wǎng)絡(luò)科學(xué)工具,它封裝了圖的數(shù)據(jù)結(jié)構(gòu)和許多經(jīng)典圖算法,也內(nèi)置了許多可視化函數(shù)可供調(diào)用。
1. 隨機(jī)圖生成
最經(jīng)典的隨機(jī)圖當(dāng)屬我們?cè)谏弦黄┛?a href="http://www.fzitv.net/article/247983.htm" rel="external nofollow" target="_blank">《Erdos-Renyi隨機(jī)圖的生成方式及其特性》中講到的Erd?s-Rény隨機(jī)圖了,我們這里選用其中的Gnp??np形式,調(diào)用以下API:
G = nx.erdos_renyi_graph(10, 0.3, seed=1)
這里表示生成10個(gè)頂點(diǎn)的圖,且圖的每條邊都以0.3的概率產(chǎn)生。
當(dāng)然,此時(shí)生成的圖不具有權(quán)重,我們想在此基礎(chǔ)上均勻隨機(jī)初始化[0, 0.4]之間的權(quán)重,可以這樣寫:
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布局可視化
隨機(jī)圖生成好之后,我們就要對(duì)其進(jìn)行可視化了。首先我們需要計(jì)算每個(gè)節(jié)點(diǎn)在圖中擺放的位置,經(jīng)典的Fruchterman-Reingold force-directed 算法可以完成這個(gè)操作,對(duì)應(yīng)NetworkX中的spring_layout函數(shù):
pos = nx.spring_layout(G, iterations=20) #我們?cè)O(shè)算法迭代次數(shù)為20次
然后就可以分別繪制圖的邊、節(jié)點(diǎn)和節(jié)點(diǎn)標(biāo)簽了:
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()
繪圖結(jié)果如下:

當(dāng)然,這樣圖的權(quán)值是無法體現(xiàn)于圖上的,如果我們需要圖的權(quán)值體現(xiàn)于圖上,可以使圖中邊的寬度按照權(quán)值大小來設(shè)置:
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()
此時(shí)的繪圖結(jié)果如下:

3. 3D布局可視化
如果你覺得2D布局過于扁平,還不夠直觀地體現(xiàn)節(jié)點(diǎn)之間的拓?fù)潢P(guān)系,那你可以采用如下的代碼對(duì)圖進(jìn)行三維可視化:
# 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()
此時(shí)的繪圖結(jié)果如下:

參考
以上就是python NetworkX庫生成并繪制帶權(quán)無向圖的詳細(xì)內(nèi)容,更多關(guān)于NetworkX庫繪制帶權(quán)無向圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 如何使用python中的networkx來生成一個(gè)圖
- Python networkx中獲取圖的鄰接矩陣方式
- Python利用networkx畫圖繪制Les?Misérables人物關(guān)系
- Python networkx包的實(shí)現(xiàn)
- 使用Python的networkx繪制精美網(wǎng)絡(luò)圖教程
- python networkx 根據(jù)圖的權(quán)重畫圖實(shí)現(xiàn)
- python networkx 包繪制復(fù)雜網(wǎng)絡(luò)關(guān)系圖的實(shí)現(xiàn)
- Python 學(xué)習(xí)教程之networkx
- python中networkx函數(shù)的具體使用
相關(guān)文章
聊聊prod()與cumprod()區(qū)別cumsum()
這篇文章主要介紹了prod()與cumprod()區(qū)別cumsum(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
matplotlib設(shè)置坐標(biāo)軸標(biāo)簽和間距的實(shí)現(xiàn)
本文主要介紹了matplotlib設(shè)置坐標(biāo)軸標(biāo)簽和間距的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
解決import tensorflow導(dǎo)致jupyter內(nèi)核死亡的問題
這篇文章主要介紹了解決import tensorflow導(dǎo)致jupyter內(nèi)核死亡的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
python接口自動(dòng)化測(cè)試數(shù)據(jù)和代碼分離解析
代碼的可維護(hù)性除了代碼冗余之外還有就是數(shù)據(jù)盡量不要和代碼摻雜在一起,因?yàn)殚喿x起來會(huì)非常的凌亂;數(shù)據(jù)分離能更好的增加代碼可讀性和可維護(hù)性,也能更好的二次修改使用2021-09-09
OpenCV每日函數(shù)之BarcodeDetector類條碼檢測(cè)器
OpenCV在V4.5.3版本的contrib包中提供了一個(gè)barcode::BarcodeDetector類,用于條形碼的識(shí)別,這篇文章主要介紹了OpenCV每日函數(shù)?BarcodeDetector條碼檢測(cè)器,需要的朋友可以參考下2022-06-06

