我正在尝试配置我的Digraph调用,以便每个节点使用所需的最大宽度和高度.给定下面的图像,每个节点将是第一个节点的宽度和第二个节点的高度.我查看了fixedsize属性,但它似乎不合适.如果fixedsize设置为true,则必须指定限制.如果可能的话,我宁愿自动确定.所需的最大值取决于给定图形的节点标签,并不总是相同.
样本设置:
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'})
Run Code Online (Sandbox Code Playgroud)
fixedsize:如果为false,则节点的大小由包含其标签和图像所需的最小宽度和高度(如果有)确定,并具有margin属性指定的边距.宽度和高度也必须至少与width和height属性指定的大小一样大,这些属性指定这些参数的最小值.如果为true,则节点大小仅由width和height属性的值指定,并且不会展开以包含文本标签.如果标签(带边距)不符合这些限制,则会发出警告.如果fixedsize属性设置为shape,则width和height属性也会确定节点形状的大小,但标签可能会大得多.避免节点重叠时使用标签和形状大小,但节点的所有边都忽略标签并仅接触节点形状.如果标签太大,则不会发出警告.
编辑:一个凌乱的例子,但是一个例子.我用'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)
Run Code Online (Sandbox Code Playgroud)
您可以添加显式的固定width和height节点属性,例如
node_attr={'fixedsize': 'true',
'width': '2',
'height': '1.5',
...}
Run Code Online (Sandbox Code Playgroud)
这迫使所有节点具有相同的大小。
当然,您必须手动确定width和的“正确”值height......
| 归档时间: |
|
| 查看次数: |
2639 次 |
| 最近记录: |