简体   繁体   English

python graphviz中的子图方法?

[英]subgraph method in python graphviz?

So I am trying to use graphviz package on python and it has a method named subgraph() but I think it's different from the definition widely used in network theory.所以我试图在 python 上使用 graphviz 包,它有一个名为subgraph()的方法,但我认为它与网络理论中广泛使用的定义不同。

As far as I know, subgraph means a graph whose nodes and edges are subsets of another graph.据我所知,子图是指一个图,其节点和边是另一个图的子集。

On graphviz user guide it says:在 graphviz 用户指南上,它说:
Add the current content of the given sole graph argument as subgraph or return a context manager returning a new graph instance created with the given (name, comment, etc.) arguments whose content is added as subgraph when leaving the context manager's with-block.将给定的唯一图参数的当前内容添加为子图或返回一个上下文管理器,返回一个使用给定(名称、评论等)参数创建的新图实例,其内容在离开上下文管理器的 with 块时添加为子图。

This is an example from user guide这是用户指南中的示例

import graphviz
p = Graph(name='parent')
p.edge('spam', 'eggs')
c = Graph(name='child', node_attr={'shape': 'box'})
c.edge('foo', 'bar')

p.subgraph(c)

例子

According to the network theory, graph p should have all nodes 'spam', 'eggs', 'foo', 'bar' and subgraph c should be made from nodes and edges used in graph p.根据网络理论,图 p 应该包含所有节点 'spam', 'eggs', 'foo', 'bar' 并且子图 c 应该由图 p 中使用的节点和边组成。

But it's not.但事实并非如此。 It seems like subgraph() method just adds two graphs into one.似乎 subgraph() 方法只是将两个图合二为一。 Am I right?我对吗?

And what is a special cluster subgraph?什么是特殊的簇子图? (If the name of a subgraph begins with 'cluster' the layout engine will treat it as a special cluster subgraph). (如果子图的名称以“集群”开头,布局引擎会将其视为特殊的集群子图)。 I can't find any result in google.我在谷歌中找不到任何结果。

Thank you谢谢

You can treat subgraph as tool for logically groupping nodes and edges.您可以将subgraph视为对节点和边进行逻辑分组的工具。

Consider following example (if you don't mind, I will be using native Graphviz syntax, not the Python Graphviz wrapper):考虑以下示例(如果您不介意,我将使用原生 Graphviz 语法,而不是 Python Graphviz 包装器):

digraph {
    a -> b
    c
    d -> e
    f
    g -> h
    c -> a
}

原来的

Now let's add a subgraph around nodes c , d and e and change the global node attributes, for example, give them rectangular shape and red color:现在让我们在节点cde周围添加一个子图并更改全局节点属性,例如,给它们矩形形状和红色:

digraph {
    a -> b
    subgraph mysubgraph {
        node [shape=rect color=red]
        c
        d -> e
    }
    f
    g -> h
    c -> a
}

子图

As you can see, nothing changed in node placement, but those nodes which were inside subgraph changed shape and color.如您所见,节点放置没有任何变化,但是子图中的那些节点改变了形状和颜色。 Also, notice that you were right: we affected both c node, which is defined as a node, and d;e nodes, which were implicitly defined as an edge.另外,请注意您是对的:我们影响了定义为节点的c节点和隐式定义为边的d;e节点。

We can also use subgraphs to control node placement (and that's what subgraphs are used for most of the times) with rank attribute.我们还可以使用子图通过rank属性来控制节点放置(这也是大多数时候使用的子图)。 To force nodes in subgraph to appear on the same line, add a rank=same attribute to your subgraph:要强制子图中的节点出现在同一行上,请向子图中添加rank=same属性:

digraph {
    a -> b
    subgraph mysubgraph {
        node [shape=rect color=red]
        rank=same
        c
        d -> e
    }
    f
    g -> h
    c -> a
}

等级相同

Clusters are a completely different story.集群是一个完全不同的故事。 When you add a word "cluster" to the beginning of your subgraph name, the nodes which are defined inside this cluster will be physically gathered together.当您在子图名称的开头添加“集群”一词时,在该集群内定义的节点将物理聚集在一起。 You will also get a rectangle (by default) wrapping these nodes on your graph:您还将获得一个矩形(默认情况下)将这些节点包裹在图形上:

digraph {
    a -> b
    subgraph cluster_mysubgraph {
        node [shape=rect color=red]
        c
        d -> e
    }
    f
    g -> h
    c -> a
}

簇

Notice the difference between this, and second picture.注意这张图和第二张图的区别。 I suggest you to use clusters with caution.我建议您谨慎使用集群。 Once you have connections between nodes inside and outside your cluster, things may go weird in terms of layout.一旦在集群内外的节点之间建立了连接,就布局而言,事情可能会变得很奇怪。

By the way, it's really hard to learn garphviz by official docs, becuase they are more of a reference rather than guide.顺便说一句,通过官方文档学习 garphviz 真的很难,因为它们更像是参考而不是指南。 But they also have a nice guide, written in good language for normal people (sorry, mathematicians) but it's buried inside Graphviz website: https://graphviz.gitlab.io/_pages/pdf/dotguide.pdf但他们也有一个很好的指南,用普通人的好语言(对不起,数学家)写的,但它埋在 Graphviz 网站内: https : //graphviz.gitlab.io/_pages/pdf/dotguide.pdf

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

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