python 使用 pydot 在 Graphviz 中垂直放置节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2215461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:05:15  来源:igfitidea点击:

Placing nodes vertically in Graphviz using pydot

pythonvisualizationgraphvizpydot

提问by hekevintran

I am using Graphviz in Python via pydot. The diagram I am making has many clusters of directed graphs. pydot is putting them next to each other horizontally resulting in an image that is very wide. How can I tell it to output images of a maximum width so that I can scroll vertically instead?

我通过 pydot 在 Python 中使用 Graphviz。我正在制作的图表有许多有向图集群。pydot 将它们水平放置在一起,导致图像非常宽。如何告诉它输出最大宽度的图像,以便我可以垂直滚动?

回答by doug

There are several things you can do.

您可以做几件事。

  1. You can set the maximum size of your graph, using 'size' (e.g., size = "4, 8" (inches)). This fixes the size of your final layout. Unlike most other node,edge, and graph parameters in the dot language, 'size' has no default. Also, the default orientation is 'portrait', which i believe is what you want (for a graph that is taller vs. wider), but you might want to set this parameter explicitly in case it was set to 'landscape' earlier.

  2. 'Size' can be used with the 'ratio' parameter (the layout aspect ratio) to manipulate the configuration. 'Ratio' takes a float (e.g., ratio = "2.0") or 'auto' or 'fill'. (The latter tells graphviz to fill use the entire graph region alloted by 'size'.

  3. The parameters that have the greatest effect on graph configuration are 'nodesep' and 'ranksep'. These are the minimumhorizontal distance between adjacent nodes of equal 'rank', and the minimumvertical distance between adjacent ranks of nodes. The default values are 0.25 and 0.75 inches, respectively. To get the configuration you want, you will want to simultaneously increase nodesep and decrease ranksep. Gradual iteration should allow you to quickly converge on a set of values for these two parameters that gives you the configuration you want.

  1. 您可以使用“size”设置图形的最大尺寸(例如,size = "4, 8" (inches))。这将修复最终布局的大小。与点语言中的大多数其他节点、边和图形参数不同,“大小”没有默认值。此外,默认方向是“纵向”,我相信这就是您想要的(对于更高与更宽的图形),但您可能需要明确设置此参数,以防它之前设置为“横向”。

  2. 'Size' 可以与 ' ratio' 参数(布局纵横比)一起使用来操纵配置。'Ratio' 采用浮点数(例如, ratio = "2.0")或 'auto' 或 'fill'。(后者告诉 graphviz 填充使用由“大小”分配的整个图形区域。

  3. 对图形配置影响最大的参数是“ nodesep”和“ ranksep”。这些是相同“等级”的相邻节点之间的最小水平距离,以及相邻节点等级之间的最小垂直距离。默认值分别为 0.25 和 0.75 英寸。要获得您想要的配置,您需要同时增加 nodeep 和减少ranksep。渐进迭代应该允许您快速收敛这两个参数的一组值,从而为您提供所需的配置。

回答by Michel Müller

Initialize your graph like this:

像这样初始化你的图表:

graph = pydot.Dot(graph_type='digraph', rankdir='LR')

graph = pydot.Dot(graph_type='digraph', rankdir='LR')

This will set the graph direction from left to right. In general, use the graphviz documentationto find the right attribute in order to achieve what you want.

这将设置从左到右的图形方向。通常,使用graphviz 文档找到正确的属性以实现您想要的。

回答by Ross Rogers

I'm not sure if you're able to do this with your data, but if you change the order that the nodes are inserted into the graph it can really affect the generated graph. If you don't want to supply anyordering information to Graphviz and want Graphviz to attempt solving optimal placement of nodes to minimize contention, use Graphviz's neatoinstead. It uses a spring model to figure out where nodes should be placed.

我不确定您是否能够对您的数据执行此操作,但是如果您更改节点插入图形的顺序,它确实会影响生成的图形。如果您不想向 Graphviz提供任何排序信息并希望 Graphviz 尝试解决节点的最佳放置以最小化争用,请改用 Graphviz neato。它使用弹簧模型来确定应该放置节点的位置。

It looks like you should be able to use neatoinside pydot like:

看起来您应该能够neato在 pydot 中使用,例如:

my_graph.write('my_graph.png', prog='neato', format='png')

See pydot's documenation here.

在此处查看 pydot 的文档