【问题标题】:Grouping nodes with the same color near each other in graphviz在graphviz中将具有相同颜色的节点分组在一起
【发布时间】:2013-11-09 13:23:39
【问题描述】:

我用 networkx 创建了一个图形,并将图形表示写入一个点文件,以便用 graphviz 显示。现在,节点具有颜色属性,我希望 graphviz 将具有相同颜色的节点彼此靠近。

例如,如果节点“soccer”和节点“football”的颜色都为“蓝色”,那么它们应该靠近在一起,而颜色为“绿色”的节点“baseball”不会靠近节点“soccer”和“football” "

  1. 如何让相同颜色的节点在 Graphviz 中靠得更近;从而形成颜色簇?

感谢所有帮助,如果您需要更多信息,请告诉我:)

【问题讨论】:

    标签: visualization graphviz graph-visualization


    【解决方案1】:

    您可以使用 PyGraphviz 使用带有“簇”的点进行布局。 例如

    import networkx as nx
    
    G = nx.Graph()
    G.add_node(1, color='blue', style='filled')
    G.add_node(2, color='red', style='filled')
    G.add_edge(1,2)
    G.add_node(3, color='blue',style='filled')
    G.add_node(4, color='red',style='filled')
    G.add_edge(3,4)
    G.add_edge(4,10)
    G.add_path([10,20,30,40,50])
    
    A = nx.to_agraph(G) # uses pygraphviz
    red_nodes = [n for n,d in G.node.items() if d.get('color')=='red']
    blue_nodes = [n for n,d in G.node.items() if d.get('color')=='blue']
    A.add_subgraph(red_nodes, name = 'cluster1', color='red')
    A.add_subgraph(blue_nodes, name = 'cluster2', color='blue')
    A.write('colors.dot')
    A.layout('dot')
    A.draw('colors.png')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 2019-10-14
      • 2017-10-24
      • 1970-01-01
      相关资源
      最近更新 更多