简体   繁体   English

如何使用python可视化节点分为2个集群的图形,边缘应该存在于集群之间和集群内?

[英]How to visualize a graph with nodes divided in 2 clusters, edges should be present between the clusters and within the clusters , using python?

I wanted to generate a graph that looks similar to this.我想生成一个与此类似的图表。

在此处输入图片说明

But For my code, I'm getting a graph like the one below但是对于我的代码,我得到了如下图

在此处输入图片说明

I have entered the following code by importing graphviz.我通过导入graphviz输入了以下代码。 I request somebody to help me make changes in it to get the required code.我请求有人帮助我对其进行更改以获得所需的代码。

from graphviz import Graph

g = Graph('G', filename='cluster.gv')
a=['1','2','3','4']
b=['5','6','7','8']

with g.subgraph(name='cluster_0') as c:
    c.attr(color='lightgrey')
    c.node_attr.update(style='filled', color='red')
    for i in a:
        c.node(str(i),shape='circle')
    c.attr(label='partition #1')

with g.subgraph(name='cluster_1') as c:
    c.attr(color='lightgrey')
    c.node_attr.update(style='filled', color='blue')
    for i in b:
        c.node(str(i),shape='circle')
    c.attr(label='partition #2')

g.edge('1','5')
g.edge('1','3')
g.edge('2','7')
g.edge('5','8')
g.view()

Thanks in advance提前致谢

  • I don't use the Python api so I don't know if it will support my suggestion.我不使用 Python api,所以我不知道它是否会支持我的建议。
  • 'Similar' is a very loose requirement. “相似”是一个非常宽松的要求。
  • That said:那说:
    Graphviz allows you to explicitly set the node positions (see here https://graphviz.org/faq/#FaqDotWithNodeCoords ). Graphviz 允许您显式设置节点位置(请参阅此处https://graphviz.org/faq/#FaqDotWithNodeCoords )。 If you want collections of ovals that combine to form an oval, you can do so.如果您想要组合形成一个椭圆的椭圆集合,您可以这样做。 Note that 'pos' values are in points while node sizes are in inches!请注意,'pos' 值以磅为单位,而节点大小以英寸为单位!

This program:这个程序:

digraph ovaltest {
 graph [splines=line]

  node [color=red shape=box style=rounded height=.2 width=.2  ]
   p1_1 [pos="70,95" ]
   p1_2 [pos="120,95" ]
   p1_3 [pos="170,95" ]
   p1_4 [pos="220,95" ]
   p2_1 [pos="70,70" ]
   p2_4 [pos="220,70" ]
   p3_1 [pos="70,45" ]
   p3_4 [pos="220,45" ]
   p4_1 [pos="70,20" ]
   p4_2 [pos="120,20" ]
   p4_3 [pos="170,20" ]
   p4_4 [pos="220,20" ]
  node [color=blue shape=box style=rounded height=.2 width=.2  ]
   q1_1 [pos="350,95" ]
   q1_2 [pos="400,95" ]
   q1_3 [pos="450,95" ]
   q2_1 [pos="350,70" ]
   q2_3 [pos="450,70" ]
   q3_1 [pos="350,45" ]
   q3_3 [pos="450,45" ]
   q4_1 [pos="350,20" ]
   q4_2 [pos="400,20" ]
   q4_3 [pos="450,20" ]

  p2_1 -> p3_4 [dir="none"]
  p2_4 -> q1_2 [dir="none"]
  p2_4 -> q3_3 [dir="none"]
  p4_4 -> q4_1 [dir="none"]
}

And this command line:这个命令行:
neato -n -Tpng yourfile.gv >yourfile.png neato -n -Tpng yourfile.gv >yourfile.png

produces this graph :产生这个图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM