使用标签显示networkx图

Mar*_*rie 10 python networkx

我正在尝试使用networkx创建带标签的图形,但是无法正确地获取节点和标签.简而言之,标签不会排列在右侧节点上,并且有些节点在显示时没有边缘.

首先,我创建了一个图形,添加了节点和边,然后添加了标签.

图形数据来自pandas DataFrame对象,其中包含两个列:employee和manager名称:

                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
Run Code Online (Sandbox Code Playgroud)

...

每个节点都是名称,边是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()
Run Code Online (Sandbox Code Playgroud)

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

输出图像是 在此输入图像描述

A_A*_*A_A 16

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

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

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

由前缀的功能draw_networkx接着edges,nodes,edge_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()
Run Code Online (Sandbox Code Playgroud)

您也可以通过命令行直接尝试使用graphviz的点,通过保存您的networkx网络nx.write_dot.去做这个:

在你的python脚本中:

nx.write_dot(G, 'test.dot')
Run Code Online (Sandbox Code Playgroud)

在此之后,从您的(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.
Run Code Online (Sandbox Code Playgroud)

对于更典型的有机图格式,您可以强制执行正交边缘路由

dot test.dot -Tpng -Gsplines=ortho>test_output.png
Run Code Online (Sandbox Code Playgroud)

最后,这是输出

输出 draw_networkx 输出<code>draw_graphviz</code></strong>
<img rel=

希望这可以帮助.