使用 pydot 在 Graphviz 中垂直放置节点
我通过 pydot 在 Python 中使用 Graphviz。我正在制作的图表有许多有向图簇。 pydot 将它们水平放置在一起,从而产生非常宽的图像。我怎样才能告诉它输出最大宽度的图像,以便我可以垂直滚动?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以做几件事。
您可以使用“size”设置图表的最大尺寸(例如,size =“4, 8”(英寸))。这会修复最终布局的大小。与点语言中的大多数其他节点、边和图参数不同,“大小”没有默认值。另外,默认方向是“纵向”,我相信这就是您想要的(对于更高与更宽的图形),但您可能需要明确设置此参数,以防它之前设置为“横向”。
“Size”可以与“ratio”参数(布局纵横比)一起使用来操纵配置。 “Ratio”采用浮点数(例如,ratio =“2.0”)或“auto”或“fill”。 (后者告诉 graphviz 填充使用“size”分配的整个图形区域。
参数对图形配置影响最大的是“nodesep”和“ranksep”,它们是相等“rank”的相邻节点之间的最小水平距离。 ',相邻节点之间的最小垂直距离默认值分别为 0.25 和 0.75 英寸。要获得所需的配置,您需要同时增加和减少节点间隔。逐步迭代应该允许您快速收敛这两个参数的一组值,从而为您提供所需的配置
There are several things you can do.
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.
'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'.
The parameters that have the greatest effect on graph configuration are 'nodesep' and 'ranksep'. These are the minimum horizontal distance between adjacent nodes of equal 'rank', and the minimum vertical 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.
像这样初始化你的图表:
graph = pydot.Dot(graph_type='digraph',rankdir='LR')
这会将图表方向设置为从左到右。一般来说,使用 graphviz 文档 找到正确的属性以实现什么你想要的。
Initialize your graph like this:
graph = pydot.Dot(graph_type='digraph', rankdir='LR')
This will set the graph direction from left to right. In general, use the graphviz documentation to find the right attribute in order to achieve what you want.
我不确定您是否能够对数据执行此操作,但如果您更改节点插入图形的顺序,它确实会影响生成的图形。如果您不想向 Graphviz 提供任何排序信息,并希望 Graphviz 尝试解决节点的最佳放置问题以最大程度地减少争用,请改用 Graphviz 的
neato
。它使用弹簧模型来确定节点应该放置在哪里。看起来你应该能够在 pydot 中使用
neato
,如下所示:请参阅 pydot 的文档 这里。
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 any ordering information to Graphviz and want Graphviz to attempt solving optimal placement of nodes to minimize contention, use Graphviz's
neato
instead. It uses a spring model to figure out where nodes should be placed.It looks like you should be able to use
neato
inside pydot like:See pydot's documenation here.