I want to arrange the nodes in such a manner that there are 2 clusters A and B.
B has further 2 clusters C and D.
For the same i wrote the below program in which i am adding 2 clusters inside one and then adding cluster B as a subgraph in main graph.
However the nested boxes do not appear. Can someone tell me what am i doing wrong ?
import pydot
d开发者_如何学Goef 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 does not support clusters, as it uses a spring model where node placement is very dynamic. If you replace neato
engine with dot
engine, your clusters will appear correcly.
精彩评论