rustworkx.PyGraph.to_dot#

PyGraph.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 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)

  • 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[str, str]) – An optional dictionary that specifies any graph attributes for the output 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)

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

Returns:

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

Return type:

字符串

使用此方法使您能够利用graphviz可视化一个rustworkx.PyGraph对象。例如:

import os
import tempfile

import pydot
from PIL import Image

import rustworkx as rx

graph = rx.undirected_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.PyGraph.to_dot_0_0.png