python - GRAPHVIZ:强制节点位于页面顶部

标签 python graphviz pygraphviz

我正在使用 graphviz,但是我想将节点“This on top”强制到页面顶部,而不是侧面。这是图表:

enter image description here

这是代码:

g= Digraph('trial', filename='trial.gv')
g.attr(compound='true', rankdir="TB" )

with g.subgraph() as s:
  s.attr(rank='max')
  s.node('This on top ')
  s.edge('this right under', "Fabrication")

with g.subgraph(name='cluster0') as c:
    c.node("This")

    c.node("that")
    c.node("and this on the same level")
g.edge("this right under","that", lhead="cluster0" )
g.edge("that","This on top ", ltail="cluster0" )

g

是否有命令可以确保节点按我希望的顶部/底部顺序显示?

最佳答案

第一个问题是设置rank='max'会强制第一个子图中的所有内容达到最大排名,即最低排名。您可能打算设置 rank='min' ,这会将子图中的项目置于最高排名,但这仍然无法创建您想要的排列。

相反,您可以通过在创建边缘时设置 style = 'invis' 来使用不可见边缘,以强制“This on top”位于“this right under”之前。

from graphviz import Digraph

g= Digraph('trial', filename='trial.gv')
g.attr(compound='true', rankdir="TB" )

with g.subgraph() as s:
  # s.attr(rank='min') # you don't need this line
  s.node('This on top ')
  s.edge('This on top ', 'this right under', style='invis') # add this invisible edge
  s.edge('this right under', "Fabrication")

with g.subgraph(name='cluster0') as c:
    c.node("This")

    c.node("that")
    c.node("and this on the same level")
g.edge("this right under", "that", lhead="cluster0" )
g.edge("that", "This on top ", ltail="cluster0", constraint="false" )

g

它产生: Graph showing nodes stacked

关于python - GRAPHVIZ:强制节点位于页面顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64297587/

相关文章:

python - 在 Windows 上使用 python 截屏的最快方法

存在集群时的 GraphViz 水平顺序

python - 在 Os X 10.5.8 上安装 Graphviz

python - 当节点数量巨大时,用Python(使用pygraphviz)创建一个图

python - 绘制 95% 置信区间 errorbar python pandas dataframes

python - env.password 在 fab 文件中设置,但进程仍然多次询问 sudo 密码

python - 将二进制 64 位时间戳偏移量从 GPS 纪元转换为 python 日期时间对象

command-line - 在不创建中间文件的情况下显示来自 Graphviz 的图像?

pandas - 使用 Graphviz 绘制决策树时出现“特征名称长度与特征数量不匹配”错误

python - 如何使用 PyGraphviz 在无向图的边上添加和显示权重?