Pygraphviz / networkx set node level or layer

后端 未结 2 1246
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 06:01

I have a dataset that represents a kind of genealogy tree. Each node has 2 parents (except first generation, they have no parents). For a given node, its parents can be from

相关标签:
2条回答
  • 2020-12-16 06:21

    As of 2017, the function to_agraph is no longer exposed at the nx.level. Now you must call nx.nx_agraph.to_agraph()

    0 讨论(0)
  • 2020-12-16 06:22

    Use a Graphviz subgraph with attribute rank=same. e.g.

    import networkx as nx
    import pygraphviz as pgv # pygraphviz should be available
    
    G = nx.DiGraph()
    G.add_edge('a','aa')
    G.add_edge('a','ab')
    G.add_edge('a','bbc')
    G.add_edge('b','ab')
    G.add_edge('b','bb')
    G.add_edge('c','bbc')
    G.add_edge('bb','bba')
    G.add_edge('bb','bbc')
    A = nx.to_agraph(G)
    one = A.add_subgraph(['a','b','c'],rank='same')
    two = A.add_subgraph(['aa','ab','bb'],rank='same')
    three = A.add_subgraph(['bba','bbc'],rank='same')
    A.draw('example.png', prog='dot')
    

    enter image description here

    0 讨论(0)
提交回复
热议问题