rustworkx.PyDAG.to_dot#

PyDAG.to_dot(node_attr=None, edge_attr=None, graph_attr=None, filename=None)#

从图生成dot文件

Parameters:
  • node_attr – A callable that will take in a node data object and return a dictionary of attributes to be associated with the node in the dot file. The key and value of this dictionary must be strings. If they’re not strings rustworkx will raise TypeError (unfortunately without an error message because of current limitations in the PyO3 type checking)

  • edge_attr – A callable that will take in an edge data object and return a dictionary of attributes to be associated with the node in the dot file. The key and value of this dictionary must be a string. If they’re not strings rustworkx will raise TypeError (unfortunately without an error message because of current limitations in the PyO3 type checking)

  • graph_attr (dict) - 一个可选字典,用于指定输出dot文件的任何图属性。该字典的键与值必须是字符串类型。如果不是字符串,rustworkx将抛出类型错误(由于PyO3类型检查的当前限制,错误信息可能无法显示)

  • filename (str) – 一个可选的路径,用于将dot文件写入 如果指定了该参数,函数将没有返回值

Returns:

如果未指定文件名,则返回包含dot文件内容的字符串。

Return type:

字符串

使用此方法能够利用graphviz来可视化一个rustworkx.PyDiGraph对象。例如:
import os
import tempfile

import pydot
from PIL import Image

import rustworkx as rx

graph = rx.directed_gnp_random_graph(15, .25)
dot_str = graph.to_dot(
    lambda node: dict(
        color='black', fillcolor='lightblue', style='filled'))
dot = pydot.graph_from_dot_data(dot_str)[0]

with tempfile.TemporaryDirectory() as tmpdirname:
    tmp_path = os.path.join(tmpdirname, 'dag.png')
    dot.write_png(tmp_path)
    image = Image.open(tmp_path)
    os.remove(tmp_path)
image
../_images/rustworkx.PyDAG.to_dot_0_0.png