显示带有标签的networkx图形

2024-05-02 00:48:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

。。。

每个节点是一个名称,边是mgr_name to 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


Tags: namenetworkx名称add图形节点标签nodes
1条回答
网友
1楼 · 发布于 2024-05-02 00:48:24

Networkx有许多绘制图表的功能,但也允许用户对整个过程进行精细控制。

draw是basic,它的docstring特别提到:

Draw the graph as a simple representation with no nodeabels or edge labels and using the full Matplotlib figure areas labels by default. See draw_networkx() for more fatured drawing that allows title, axis labels

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

当使用draw_networkx时,您的示例运行良好。

此外,如果您正在寻找类似于有机图的输出,我建议通过networkx使用graphviz。Graphviz的dot是这类图的理想选择(对于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_networkxOutput of <code>draw_networkx</code>

输出draw_graphvizOutput of <code>draw_graphviz</code>

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

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

希望这有帮助。

相关问题 更多 >