Graphviz:避免重叠边和节点

时间:2016-05-12 18:10:09

标签: python nodes graphviz edges

我知道这是一个经常出现的问题,但在我的案例中我无法找到合适的答案。 我有一个包含365个节点的图表,可以正确绘制。tree 目前,结果几乎令人满意,但并不完美。如您所见,一些边缘相互交叉,一些节点重叠。我想避免这种情况,而且我很确定它是可能的,有更多的可用空间可供更好的组织使用。我在NetworkX中使用了graphviz_layout函数。你有什么想法吗?

这是一个有效的例子:

import networkx as nx
import matplotlib.pyplot as plt
import re
import pygraphviz
from networkx.drawing.nx_agraph import graphviz_layout

G = nx.Graph()
G.add_node(1)

inp = "BDDDDDDDDDCCDDCh14CCCh13Ch10Ce24CCh9DCCCCh28CCh8DDDDDDDCCCCCh43Ch42CCCCh41DDDh59Ch58CCCh57CCh40CCDCCh72CCCCCCh39CDDCCh85Ch84h38Ch37DDDCh96Ch95h94CCCh7CDDDDDCDCDh115CDCCh118CCh113Dh125Ch111CDe131CCCh130h110Ch109DDCCCCh140CCh139CCCCh108CCCh107DDh159CCCh158CCCh6DDDDDDCh174CCCh173DCDCDDCCCh186CCh185CCh183CCh181Ch172Ch171DDDCCCh206CCh205CCh204Ch170CCCCh169CCCh5DDDCCCh230DDe237CCCh236CCCh235Ch229CCCCh228CCCCCCCh4CCCh3DDDCDDCCh270Ch269DDh277h276CCh267CCCh266DDCCh288CCCh287CCCCCh265DCCCCh302CCh2CCCCCh1DCCDDDh322Ch321DDCe329CCCCCCCCh327CCh326CCCCCh320CCDCCCh350CCCh317DCe361Ch359Ch"

chains = re.findall("([a-zA-Z]+)", inp)
bp = [int(value) for value in re.findall("([0-9]+)", inp)]

while G.number_of_nodes() < len(chains[0]):
    G.add_node(G.number_of_nodes() + 1)
    G.add_edge(G.number_of_nodes(), G.number_of_nodes() - 1)

for number, sequence in zip(bp, chains[1:]):
    G.add_node(G.number_of_nodes() + 1)
    G.add_edge(number, G.number_of_nodes())

    for res in sequence[:-1]:
        G.add_node(G.number_of_nodes() + 1)
        G.add_edge(G.number_of_nodes(), G.number_of_nodes() - 1)

pos = nx.nx_agraph.graphviz_layout(G)

fig = plt.figure(1)

nx.draw(G, pos)

fig.set_size_inches(50, 50)
plt.savefig('tree.png', bbox_inches='tight')

0 个答案:

没有答案