NetworkX中的网络图未进行可视化优化

2024-04-24 04:45:26 发布

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

我不熟悉使用NetworkX,所以我可能做错了什么。我尝试使用从wikipedia.org中获取的数据创建简单的图形。下面是我使用spring_layout选项构建的一个简单图形的示例。这是预期的产出吗?我想它会重新安排点,尽量避免交叉线,这样看起来更简单。它似乎一点也不想避开过境点。在

另外,我想要更多从左到右的流程图,比如this(每个点都有一个年份值)(或者垂直),但是这个doesn't seem to be possible in NetworkX。有人能证实吗?在我看来,线性流程图是一种常见的需求。在

enter image description here

本例中的数据:

selected_nodes = [96, 64, 163, 132, 166, 138, 108, 141, 238, 50, 58, 60, 61, 223]

selected_edges = [
    (50, 58),
    (61, 64),
    (60, 64),
    (58, 96),
    (108, 132),
    (96, 141),
    (138, 163),
    (141, 163),
    (64, 166),
    (163, 223),
    (132, 238),
    (96, 238),
    (166, 238),
    (223, 238)
]

text_labels = {
    50: u'ALGOL 58 (IAL)',
    58: u'ALGOL 60',
    60: u'COMIT (implementation)',
    61: u'FORTRAN IV',
    64: u'SNOBOL',
    96: u'ALGOL 68 (UNESCO/IFIP standard)',
    108: u'SETL',
    132: u'ABC',
    138: u'Modula',
    141: u'Mesa',
    163: u'Modula-2',
    166: u'Icon (implementation)',
    223: u'Modula-3',
    238: u'Python'
}

脚本代码

^{pr2}$

Tags: 数据orgnetworkx图形示例选项wikipedia交叉
1条回答
网友
1楼 · 发布于 2024-04-24 04:45:26
The graphviz dot layout has a hierarchical layout.  If you install pygraphviz you can use it like this

import networkx as nx
import matplotlib.pyplot as plt
from networkx.drawing.nx_agraph import to_agraph

selected_nodes = [96, 64, 163, 132, 166, 138, 108, 141, 238, 50, 58, 60, 61, 223]

selected_edges = [
    (50, 58),
    (61, 64),
    (60, 64),
    (58, 96),
    (108, 132),
    (96, 141),
    (138, 163),
    (141, 163),
    (64, 166),
    (163, 223),
    (132, 238),
    (96, 238),
    (166, 238),
    (223, 238)
]

text_labels = {
    50: u'ALGOL 58 (IAL)',
    58: u'ALGOL 60',
    60: u'COMIT (implementation)',
    61: u'FORTRAN IV',
    64: u'SNOBOL',
    96: u'ALGOL 68 (UNESCO/IFIP standard)',
    108: u'SETL',
    132: u'ABC',
    138: u'Modula',
    141: u'Mesa',
    163: u'Modula-2',
    166: u'Icon (implementation)',
    223: u'Modula-3',
    238: u'Python'
}
G = nx.DiGraph() # Create an empty Graph

for k,v in text_labels.items():
    G.add_node(k,label=v)
G.add_edges_from(selected_edges)

A = to_agraph(G)

A.draw('lang_predecessors.png', prog='dot')

enter image description here

相关问题 更多 >