使用标签显示networkx图

时间:2015-04-20 02:13:19

标签: python networkx

我正在尝试使用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()

理想情况下,我会有一个树状结构,员工姓名作为每个节点的标签。

输出图像为enter image description here

1 个答案:

答案 0 :(得分:14)

Networkx有许多绘制图形的功能,但也允许用户精确控制整个过程。

draw是基本的,其文档字符串具体提及:

  

将图形绘制为没有节点标签或边缘的简单表示   标签并默认使用完整的Matplotlib图形区域标签。   请参阅draw_networkx()以获取更多有关标题,轴的图像   标签

draw_networkx为后缀edgesnodesedge_labelsedge_nodes的函数可以更好地控制整个绘图过程。

使用draw_networkx时,您的示例运行正常。

此外,如果您正在寻找类似于组织图的输出,我建议通过networkx使用graphviz。 Graphviz的dot非常适用于此类图表(请点击see this)。

在下文中,我尝试稍微修改您的代码以演示这两个函数的使用:

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 的输出 Output of <code>draw_networkx</code>

draw_graphviz 的输出 Output of <code>draw_graphviz</code>

dot没有正交边的输出 Output of <code>dot</code> without orthogonal edges

带有正交边的dot输出 Output of <code>dot</code> with orthogonal edges

希望这有帮助。