簡體   English   中英

顯示帶有標簽的networkx圖

[英]Displaying networkx graph with labels

我正在嘗試使用networkx創建一個帶標簽的圖形,但是在使節點和標簽正確顯示時遇到了麻煩。 簡而言之,標簽不在正確的節點上排列,並且有些節點在顯示時沒有邊緣。

首先,我創建了一個圖,添加了節點和邊,然后添加了標簽。

該圖數據來自具有兩列的pandas DataFrame對象,即雇員和經理姓名:

                emp_name             mgr_name
0        Marianne Becker                 None
1            Evan Abbott      Marianne Becker
2               Jay Page      Marianne Becker
3             Seth Reese      Marianne Becker
4         Maxine Collier      Marianne Becker

...

每個節點都是一個名稱,邊緣是mgr_name與emp_name的關系。

我的圖形代碼:

import networkx as nx
G=nx.DiGraph()

#set layout
pos=nx.spring_layout(G)

#add nodes
G.add_nodes_from(df.emp_name)
G.nodes()
G.add_node('None')

#create tuples for edges
subset = df[['mgr_name','emp_name']]
tuples = [tuple(x) for x in subset.values]

#add edges
G.add_edges_from(tuples)
G.number_of_edges()

#draw graph
import matplotlib.pyplot as plt
nx.draw(G, labels = True)
plt.show()

理想情況下,我將使用員工名稱作為每個節點的標簽的樹狀結構。

輸出圖像是 在此處輸入圖片說明

Networkx具有許多繪制圖形的功能,但也允許用戶對整個過程進行精細控制。

draw是基本的,其docstring特別提到:

默認情況下,將圖形繪制為沒有節點標記或邊緣標簽的簡單表示形式,並使用完整的Matplotlib圖形區域標簽。 請參閱draw_networkx()以獲取更多帶有標題,軸標簽的注圖

draw_networkx為前綴的函數,后跟edgesnodesedge_labelsedge_nodes可以更好地控制整個繪制過程。

使用draw_networkx時,您的示例運行良好。

另外,如果您正在尋找類似於器官圖的輸出,我建議通過networkx使用graphviz Graphviz的dot是此類圖表的理想選擇(請參見點的更多信息)。

在下面的內容中,我試圖對您的代碼進行一些修改,以演示這兩個函數的用法:

import networkx as nx
import matplotlib.pyplot as plt
import pandas

#Build the dataset
df = pandas.DataFrame({'emp_name':pandas.Series(['Marianne Becker', 'Evan Abbott', 'Jay Page', 'Seth Reese', 'Maxine Collier'], index=[0,1,2,3,4]), 'mgr_name':pandas.Series(['None', 'Marianne Becker', 'Marianne Becker', 'Marianne Becker', 'Marianne Becker'], index = [0,1,2,3,4])})

#Build the graph
G=nx.DiGraph()   
G.add_nodes_from(df.emp_name)
G.nodes()
G.add_node('None')
#
#Over here, you are manually adding 'None' but in reality
#your nodes are the unique entries of the concatenated
#columns, i.e. emp_name, mgr_name. You could achieve this by
#doing something like
#
#G.add_nodes_from(list(set(list(D.emp_name.values) + list(D.mgr_name.values))))
#
# Which does exactly that, retrieves the contents of the two columns
#concatenates them and then selects the unique names by turning the
#combined list into a set.

#Add edges
subset = df[['mgr_name','emp_name']]
tuples = [tuple(x) for x in subset.values] 
G.add_edges_from(tuples)
G.number_of_edges()

#Perform Graph Drawing
#A star network  (sort of)
nx.draw_networkx(G)
plt.show()
t = raw_input()
#A tree network (sort of)
nx.draw_graphviz(G, prog = 'dot')
plt.show()

您還可以嘗試通過nx.write_dot保存networkx網絡,直接從命令行使用graphviz的點。 去做這個:

從您的python腳本中:

nx.write_dot(G, 'test.dot')

之后,從您的(Linux)命令行並假設您已安裝graphviz:

dot test.dot -Tpng>test_output.png
feh test_output.png #Feh is just an image viewer.
firefox test_output.png & #In case you don't have feh installed.

對於更典型的器官圖格式,您可以通過以下方式強制進行正交邊緣路由:

dot test.dot -Tpng -Gsplines=ortho>test_output.png

最后,這是輸出

draw_networkx輸出 <code> draw_networkx </ code>的輸出

draw_graphviz輸出 <code> draw_graphviz </ code>的輸出

無正交邊的dot輸出 沒有正交邊的<code> dot </ code>的輸出

具有正交邊的dot輸出 具有正交邊的<code> dot </ code>的輸出

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM