第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何以可讀的方式繪制復(fù)雜的networkx DiGraph?

如何以可讀的方式繪制復(fù)雜的networkx DiGraph?

有只小跳蛙 2022-10-05 17:54:30
我想使用 networkx 來研究一個(gè)相當(dāng)大的項(xiàng)目的架構(gòu),但是到目前為止我所做的測(cè)試并不是那么好,這是我所有研究的一個(gè)最小示例:import matplotlib.pyplot as pltimport networkx as nxfrom networkx.readwrite import node_link_graphnx.draw(G, with_labels=True)plt.show()如您所見,以這種幼稚的方式繪制圖形將產(chǎn)生完全無用且不可讀的輸出,例如:?jiǎn)栴}是,在我閱讀了 networkx 教程/文檔之后,檢查了一些谷歌參考資料后,我一直無法找到完成任務(wù)的正確方法。我已經(jīng)安裝了graphviz,但是無論如何我嘗試在windows上構(gòu)建和運(yùn)行pygraphivz/pydot都失敗了……問題:如何使用 networkx 以某種層次和干凈的方式繪制復(fù)雜的圖形,其中節(jié)點(diǎn)均勻分布在它們之間?下面你可以看到我想在這里實(shí)現(xiàn)的輸出類型:如您所見,節(jié)點(diǎn)分散,循環(huán)顯示正確,層次結(jié)構(gòu)的不同級(jí)別自上而下完全清晰......如果可以使用networkx實(shí)現(xiàn)類似(或類似)的事情,那就太好了。事實(shí)上,本文所描述的正是我想在這里實(shí)現(xiàn)的輸出類型NS。從這個(gè)網(wǎng)站借來的圖片示例
查看完整描述

1 回答

?
慕桂英3389331

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊

drawnetworkx的各種函數(shù)都帶有一個(gè)pos參數(shù),該參數(shù)是一個(gè)字典,其中節(jié)點(diǎn)名稱為鍵,x,y 坐標(biāo)為值。


你可以自己生成這個(gè)。如果您知道要強(qiáng)加的層次結(jié)構(gòu),則可以將層次結(jié)構(gòu)轉(zhuǎn)換為 y 位置,然后隨時(shí)添加填充 x 位置:


# exctracting nodes from dictionary into list:

nodes = [{'id': 'build'}, {'id': 'root'}, {'id': 'utils'}, {'id': 'codegen'}, {'id': 'codegen.templates'}, {'id': 'nodes.shapes'}, {'id': 'codegen.c_types'}, {'id': 'nodes'}, {'id': 'containers'}, {'id': 'distutils'}, {'id': 'wheel'}, {'id': 'tools.testing'}, {'id': 'finalizations'}, {'id': 'importing'}, {'id': 'plugins'}, {'id': 'freezer'}, {'id': 'tree'}, {'id': 'specs'}, {'id': 'optimizations'}, {'id': 'plugins.standard'}, {'id': 'tools.general.dll_report'}, {'id': 'tools.specialize'}, {'id': 'tools.testing.compare_with_cpython'}, {'id': 'tools.testing.find_sxs_modules'}, {'id': 'tools.testing.measure_construct_performance'}, {'id': 'tools.testing.run_root_tests'}, {'id': 'tools'}]


nodelist = []

for n in nodes:

    for k, v in n.items():

        nodelist.append(v)


# hierarchy here is arbitrarily defined based on the index of hte node in nodelist. 

# {hierarchy_level : number_of_nodes_at_that_level}

hierarchy = {

    0:4,

    1:10,

    2:5,

    3:5,

    4:3

}


coords = []

for y, v in hierarchy.items():

    coords += [[x, y] for x in list(range(v))]


# map node names to positions 

# this is based on index of node in nodelist.

# can and should be tailored to your actual hierarchy    

positions = {}

for n, c in zip(nodelist, coords):

    positions[n] = c


fig = plt.figure(figsize=(15,5))

nx.draw_networkx_nodes(G, pos=positions, node_size=50)

nx.draw_networkx_edges(G, pos=positions, alpha=0.2)


# generate y-offset for the labels, s.t. they don't lie on the nodes

label_positions = {k:[v0, v1-.25] for k, (v0,v1) in positions.items()}

nx.draw_networkx_labels(G, pos=label_positions, font_size=8)

plt.show()

http://img1.sycdn.imooc.com//633d5a3a0001d58f08460282.jpg

節(jié)點(diǎn)標(biāo)簽有些重疊,但這可以通過字體大小進(jìn)行調(diào)整,通過圖形尺寸進(jìn)行額外偏移


編輯:

旋轉(zhuǎn)節(jié)點(diǎn)標(biāo)簽以避免文本重疊:


text = nx.draw_networkx_labels(G, pos=label_positions, font_size=8)

for _, t in text.items():

    t.set_rotation(20)

http://img1.sycdn.imooc.com//633d5a470001a4eb08480282.jpg

查看完整回答
反對(duì) 回復(fù) 2022-10-05
  • 1 回答
  • 0 關(guān)注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)