python - pydot 中的嵌套集群

标签 python pydot

我想以这样的方式排列节点,使得有 2 个簇 A 和 B。

B 还有 2 个簇 C 和 D。

同样,我编写了下面的程序,其中我在一个簇中添加 2 个簇,然后将簇 B 作为主图中的子图添加。

但是嵌套框不会出现。有人可以告诉我我做错了什么吗?

import pydot

def draw( ListA , ListB , ListC , filename ):
    graph = pydot.Dot(graph_type='digraph',fontsize = 50  )
    List_nodesA = []
    List_nodesB = []
    List_nodesC = []
    cluster1 = pydot.Cluster( "A" , color = ".3 .5 .7"  )
    cluster2 = pydot.Cluster( "D" , color = ".6 .5 .2"  )
    cluster3 = pydot.Cluster( "C" , color = ".7 .5 .9" )
    cluster4 = pydot.Cluster( "B" )
    for item in ListA:
        List_nodesA.append(pydot.Node(str( item ) , shape = "circle", style="filled", fillcolor="0.5 0.4 0.9" ))
    for item in ListB:
        List_nodesB.append(pydot.Node(str( item ) , shape = "circle", style="filled", fillcolor="orange"))
    for item in ListC:
        List_nodesC.append(pydot.Node(str( item ) , shape = "circle", style="filled", fillcolor="green"))

    for node in List_nodesA:
        cluster1.add_node( node )

    graph.add_subgraph( cluster1 )
    for node in List_nodesB:
        cluster2.add_node( node )  # create cluster D
    cluster4.add_subgraph( cluster2 ) # add it to B

    for node in List_nodesC:
        cluster3.add_node( node ) # create cluster C
    cluster4.add_subgraph( cluster3 ) # add it to B

    graph.add_subgraph( cluster4 )
    for vertex1 in List_nodesA:
        for vertex2 in List_nodesB:
            graph.add_edge( pydot.Edge( vertex1 , vertex2 , len=1.5 ) )

    graph.write(filename,prog = 'neato',format = 'png')

draw( [1,2] , [3,4] , [5,6] , "graph.png" )

最佳答案

Neato 不支持集群,因为它使用 spring 模型,其中节点放置非常动态。如果您将 neato 引擎替换为 dot 引擎,您的集群将正确显示。

关于python - pydot 中的嵌套集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6368920/

相关文章:

python - 获取使用 __init__ 重新创建对象所需的对象变量

python - Tensorflow 服务应该克隆到哪里?

python - 无法在抢占状态下重新启动 TPU 节点

python - 使用 nx.draw_graphviz 在 python 中的 graphviz 布局中绘制图形会出错

python-3.x - 使用 class_names 使用 graphviz 的树节点的颜色

python - 如何在Scrapy中循环响应元素?

python - Python 中的区域交集

python-3.x - 如何使用 graphviz 通过 networkx 获取图形坐标(布局)?

python - 属性错误 : module 'pydot' has no attribute 'graph_from_dot_data' in spyder

python - 使用 python pydot 时出错