gpt4 book ai didi

python - Graphviz:使所有节点的大小与最大的节点相同

转载 作者:太空狗 更新时间:2023-10-29 21:56:09 24 4
gpt4 key购买 nike

我正在尝试配置我的有向图调用,以便每个节点使用所需的最大宽度和高度。给定下图,每个节点将是第一个节点的宽度和第二个节点的高度。我查看了 fixedsize 属性,但它似乎不合适。如果 fixedsize 设置为 true,则必须指定限制。如果可能的话,我宁愿自动确定。所需的最大值将取决于给定图形的节点标签,并且不会始终相同。

enter image description here

示例设置:

dot = Digraph(comment="Example 1",
format='png',
edge_attr={'color':'black',
'style':'filled'},
graph_attr={'fixedsize':'false',
'bgcolor':'transparent'},
node_attr={'fontname':'bold',
'fontsize':'11',
'shape':'sqaure',
'color':'black',
'style':'filled',
'fillcolor':'lightsteelblue3'})

fixedsize: If false, the size of a node is determined by smallest width and height needed to contain its label and image, if any, with a margin specified by the margin attribute. The width and height must also be at least as large as the sizes specified by the width and height attributes, which specify the minimum values for these parameters. If true, the node size is specified by the values of the width and height attributes only and is not expanded to contain the text label. There will be a warning if the label (with margin) cannot fit within these limits. If the fixedsize attribute is set to shape, the width and height attributes also determine the size of the node shape, but the label can be much larger. Both the label and shape sizes are used when avoiding node overlap, but all edges to the node ignore the label and only contact the node shape. No warning is given if the label is too large.

编辑:一个困惑的例子,但仍然是一个例子。我以“gv”格式构建图表,处理高度和宽度,然后重建图表。

from graphviz import Digraph

dot = Digraph(comment="Example 1",
format='gv',
edge_attr={'color':'brown',
'style':'filled'},
graph_attr={'rankdir':'LR',
'bgcolor':'transparent'},
node_attr={'fontsize':'11',
'shape':'sqaure',
'color':'black',
'style':'filled',
'fillcolor':'antiquewhite'})
# nodes and edges
dot.node('1', 'This is the longest width')
dot.node('2', 'This\nis\nthe\nlargest\nheight')
dot.node('3', 'Small')
dot.edges(['12','23'])

def get_node_max(digraph):
import re
heights = [height.split('=')[1] for height in re.findall('height=[0-9.]+', str(digraph))]
widths = [width.split('=')[1] for width in re.findall('width=[0-9.]+', str(digraph))]
heights.sort(key=float)
widths.sort(key=float)
return heights[len(heights)-1], widths[len(widths)-1]

params = {'format':'png', 'fixedsize':'false', 'width':'1', 'height':'1'}

params['height'], params['width'] = get_node_max(dot.pipe())


dot = Digraph(comment="Example 1",
format=params['format'],
edge_attr={'color':'brown',
'style':'filled'},
graph_attr={'rankdir':'LR',
'bgcolor':'transparent'},
node_attr={'fixedsize':params['fixedsize'],
'width':params['width'],
'height':params['height'],
'fontsize':'11',
'shape':'sqaure',
'color':'black',
'style':'filled',
'fillcolor':'antiquewhite'})
# nodes and edges
dot.node('1', 'This is the longest width')
dot.node('2', 'This\nis\nthe\nlargest\nheight')
dot.node('3', 'Small')
dot.edges(['12','23'])

dot.render('example-graphviz.gv', view=True)

enter image description here

最佳答案

您可以添加明确的固定 widthheight 节点属性,例如

node_attr={'fixedsize': 'true',
'width': '2',
'height': '1.5',
...}

这会强制所有节点具有相同的大小。

当然,您必须手动确定 widthheight 的“正确”值...

关于python - Graphviz:使所有节点的大小与最大的节点相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48888032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com