networkx庫繪制帶權(quán)圖給無權(quán)圖加權(quán)重輸出
問題
最近在研究圖學習,在用networkx庫繪圖的時候發(fā)現(xiàn)問題。
'''
author:zheng
time:2020.10.23
'''
import networkx as nx
import random
g = nx.karate_club_graph() # 空手道俱樂部
for u,v in g.edges:
print(u,v)
g.add_edge(u, v, weight=random.uniform(0, 1)) # 權(quán)值為(0,1)間的隨機數(shù)
print(g.edges())
輸出結(jié)果
[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 10), (0, 11), (0, 12), (0, 13), (0, 17), (0, 19), (0, 21), (0, 31), (1, 2), (1, 3), (1, 7), (1, 13), (1, 17), (1, 19), (1, 21), (1, 30), (2, 3), (2, 7), (2, 8), (2, 9), (2, 13), (2, 27), (2, 28), (2, 32), (3, 7), (3, 12), (3, 13), (4, 6), (4, 10), (5, 6), (5, 10), (5, 16), (6, 16), (8, 30), (8, 32), (8, 33), (13, 33), (19, 33), (31, 24), (31, 25), (31, 28), (31, 32), (31, 33), (30, 32), (30, 33), (9, 33), (27, 23), (27, 24), (27, 33), (28, 33), (32, 14), (32, 15), (32, 18), (32, 20), (32, 22), (32, 23), (32, 29), (32, 33), (33, 14), (33, 15), (33, 18), (33, 20), (33, 22), (33, 23), (33, 26), (33, 29), (23, 25), (23, 29), (25, 24), (29, 26)]
發(fā)現(xiàn)了問題,我明明通過random.uniform(0, 1)隨機設(shè)置了權(quán)重為什么在結(jié)果輸出中并未顯示,是輸入權(quán)重的問題,還是結(jié)果展示的問題。
'''
author:zheng
time:2020.10.23
'''
import networkx as nx
import random
g = nx.karate_club_graph() # 空手道俱樂部
for u,v in g.edges:
g.add_edge(u, v, weight=random.uniform(0, 1)) # 權(quán)值為(0,1)間的隨機數(shù)
print(g.edges(data=True))
大家看看兩個代碼有沒有什么不同,在G.edges(data=True)中添加了data=True
此時輸出結(jié)果:
[(0, 1, {'weight': 0.49899129531032826}), (0, 2, {'weight': 0.7493395367183026}), (0, 3, {'weight': 0.9805046801748599}), (0, 4, {'weight': 0.644560549909913}), (0, 5, {'weight': 0.022461095194206915}), (0, 6, {'weight': 0.39855273941801683}), (0, 7, {'weight': 0.9167666610641618}), (0, 8, {'weight': 0.3736839965822629}), (0, 10, {'weight': 0.1685687039463848}), (0, 11, {'weight': 0.5900599708379352}), (0, 12, {'weight': 0.49772285717726605}), (0, 13, {'weight': 0.6988903320924684}), (0, 17, {'weight': 0.8108991409995218}), (0, 19, {'weight': 0.21743421569163335}), (0, 21, {'weight': 0.687637570308398}), (0, 31, {'weight': 0.13180440967486262}), (1, 2, {'weight': 0.0603379086168323}), (1, 3, {'weight': 0.9536653778354264}), (1, 7, {'weight': 0.1680232359702576}), (1, 13, {'weight': 0.23821372652905115}), (1, 17, {'weight': 0.6861169007257469}), (1, 19, {'weight': 0.006553274592374314}), (1, 21, {'weight': 0.23452495215883118}), (1, 30, {'weight': 0.7638165639559286}), (2, 3, {'weight': 0.18381620307197954}), (2, 7, {'weight': 0.08671998389998026}), (2, 8, {'weight': 0.7395899045684956}), (2, 9, {'weight': 0.5973616237830935}), (2, 13, {'weight': 0.25253256663029156}), (2, 27, {'weight': 0.4151629971620948}), (2, 28, {'weight': 0.6830413630275037}), (2, 32, {'weight': 0.10877354662752325}), (3, 7, {'weight': 0.3165078261209674}), (3, 12, {'weight': 0.3258985972202395}), (3, 13, {'weight': 0.5617183737707032}), (4, 6, {'weight': 0.9944831897451706}), (4, 10, {'weight': 0.4258447405573552}), (5, 6, {'weight': 0.17102663345956715}), (5, 10, {'weight': 0.41020894392823837}), (5, 16, {'weight': 0.24048864347638477}), (6, 16, {'weight': 0.5401785263069063}), (8, 30, {'weight': 0.4604358340149278}), (8, 32, {'weight': 0.9601569527970788}), (8, 33, {'weight': 0.2905405465193912}), (13, 33, {'weight': 0.2556445407164615}), (19, 33, {'weight': 0.3008126988319231}), (31, 24, {'weight': 0.8781944129721222}), (31, 25, {'weight': 0.392828914742127}), (31, 28, {'weight': 0.7410701847068474}), (31, 32, {'weight': 0.39869250595380246}), (31, 33, {'weight': 0.4380052794486696}), (30, 32, {'weight': 0.4587792580500568}), (30, 33, {'weight': 0.5106934704075864}), (9, 33, {'weight': 0.9037424067215868}), (27, 23, {'weight': 0.9151325306454512}), (27, 24, {'weight': 0.6079907996445639}), (27, 33, {'weight': 0.6168782680542676}), (28, 33, {'weight': 0.9529880704286767}), (32, 14, {'weight': 0.21711370788129514}), (32, 15, {'weight': 0.21906480255644156}), (32, 18, {'weight': 0.36297161231472697}), (32, 20, {'weight': 0.8295507296873654}), (32, 22, {'weight': 0.725850047579389}), (32, 23, {'weight': 0.06395474428944792}), (32, 29, {'weight': 0.021001018687274553}), (32, 33, {'weight': 0.29227780907194645}), (33, 14, {'weight': 0.7898337840851372}), (33, 15, {'weight': 0.06574640956244104}), (33, 18, {'weight': 0.3193055980182168}), (33, 20, {'weight': 0.22814267912232755}), (33, 22, {'weight': 0.934928086748862}), (33, 23, {'weight': 0.8780586608909188}), (33, 26, {'weight': 0.834765093283264}), (33, 29, {'weight': 0.8927802653939352}), (23, 25, {'weight': 0.18106036608743914}), (23, 29, {'weight': 0.7824721548411848}), (25, 24, {'weight': 0.9362577071184671}), (29, 26, {'weight': 0.06557785001633887})]
如何只輸出權(quán)重
import networkx as nx
import random
g = nx.karate_club_graph() # 空手道俱樂部
for u,v in g.edges:
g.add_edge(u, v, weight=random.uniform(0, 1)) # 權(quán)值為(0,1)
for (u,v,d) in g.edges(data=True):
print(d['weight'])
輸出結(jié)果
0.9175521740544361
0.09841104142600388
0.9557658899707079
0.9256010898041206
0.2519120041349847
0.48370396192288767
0.8354304958648846
0.758094795660556
0.7910256982243447
0.6281003207621544
0.9801420646231339
0.7941450155753779
0.3851720075568309
0.802202234860892
0.7923045754263267
0.5270583359776736
0.9523963539542339
0.7474601472346581
0.044707615637251674
0.5349188097983026
0.6158693844408302
0.9456154478628968
0.7547788968185274
0.5648525235741113
0.6657063624514532
0.3109915743055601
0.3969190047820317
0.8763009836310122
0.7101598558464499
0.012225959063178693
0.700579386399397
0.8304116006624506
0.426518724548162
0.07244870577629914
0.36116795615537345
0.45781457416039606
0.25726914791707645
0.29778955309109023
0.8892096639219873
0.39322230058450647
0.5085017515323529
0.9597980742524421
0.08034618164792517
0.9143712112937563
0.17242150180445381
0.8914706349104955
0.8480034205451665
0.8217034225251223
0.45552196009659873
0.3909280195122691
0.45119988941609357
0.02984583822414133
0.14404544949710196
0.45459370924953857
0.10296953351890004
0.4948127850493056
0.9238669854480596
0.9399144983422378
0.919211279645529
0.24084759450828674
0.4410486851096309
0.7699702465967465
0.27749525807367836
0.9449097003790671
0.5019309896062647
0.42774455164796255
0.43988066338230847
0.7405733579782761
0.2308870299365694
0.12306785713306911
0.7139426386075743
0.2640769424119722
0.031149630992576394
0.07700734539599274
0.37034537464573547
0.7034898163898959
0.8557141929947621
0.06539918397508715
以上就是networkx庫繪制帶權(quán)圖給無權(quán)圖加權(quán)重輸出的詳細內(nèi)容,更多關(guān)于networkx帶權(quán)圖無權(quán)圖輸出的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
WxPython開發(fā)之實現(xiàn)表格數(shù)據(jù)導出到Excel并打開
在 Python 中使用 wxPython 導出實體類列表數(shù)據(jù)到 Excel,通??梢越柚?nbsp;openpyxl 或 pandas 庫來實現(xiàn),下面就跟隨小編一起來了解下具體操作吧2024-12-12
python實現(xiàn)植物大戰(zhàn)僵尸游戲?qū)嵗a
這篇文章主要給大家介紹了關(guān)于python實現(xiàn)植物大戰(zhàn)僵尸游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06
Python?Jupyter?Notebook導包報錯問題及解決
在conda環(huán)境中安裝包后,Jupyter?Notebook導入時出現(xiàn)ImportError,可能是由于包版本不對應(yīng)或版本太高,解決方法包括卸載并重新安裝出錯的包,或安裝該包的其他版本,此外,確保在正確的環(huán)境中安裝ipykernel,并使用正確的命令添加和更改kernel2025-02-02
Python3.8如何解決No module named 'numpy&apos
這篇文章主要介紹了Python3.8如何解決No module named 'numpy'報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Django基礎(chǔ)知識 web框架的本質(zhì)詳解
這篇文章主要介紹了Django基礎(chǔ)知識 web框架的本質(zhì)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07

