python中的图形渲染(流程图可视化)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4468364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:52:40  来源:igfitidea点击:

graph rendering in python (flowchart visualization)

pythongraphdata-visualizationgraphviz

提问by eWizardII

to visualize a sequence of nodes connected by edges encoded in python.

可视化由python编码的边连接的节点序列。

looking for a python library to visualize such graph data.

寻找一个python库来可视化这样的图形数据。

either a library written in python or python bindings, is ok

用 python 或 python 绑定编写的库都可以

(i am aware of Visustin, but looking for alternatives)

(我知道 Visustin,但正在寻找替代品)

采纳答案by doug

Graphvizis the best option in my opinion.

Graphviz在我看来是最好的选择。

Graphvizis the premiere graph rendering/layout library; it's mature, stable, open-source, and free of charge. It is not a dedicated flowchart or diagramming package, but its core use case--i.e., efficient and aesthetic rendering of objects comprised of nodes and edges, obviously subsumes flowchart drawing--particularly because its api allows the user to set various constraintson the layout to encourage rendering in the various formats--eg, you can require all nodes of the same level (same number of parents from the root) to be rendered in a single center-aligned row.

Graphviz是首屈一指的图形渲染/布局库;它成熟、稳定、开源且免费。它不是一个专用流程图或图表包,但其核心用例-由节点和边缘的物体的,即,有效的和美观的渲染,显然涵括流程图绘制-特别是由于它的API允许用户设置各种约束上布局以鼓励以各种格式呈现 - 例如,您可以要求在单个居中对齐的行中呈现相同级别的所有节点(来自根的相同数量的父节点)。

Graphvizis not a python library (it's written in C); however there are high quality python bindings available.

Graphviz不是 Python 库(它是用 C 编写的);但是有高质量的 python 绑定可用。

The python-Graphviz library I am most familar with is pygraphviz, which is excellent.

我最熟悉的 python-Graphviz 库是pygraphviz,非常好。

The other two are pydotand yapgvb. I have used both of these at least a few times. Each is smaller than pygraphviz (which might be an advantage depending on your use case); in addition neither is documented as well as pygraphviz.

另外两个是pydotyapgvb。我至少使用过这两种方法几次。每个都小于 pygraphviz(根据您的用例,这可能是一个优势);此外,pygraphviz 和 pygraphviz 都没有记录。

Fortunately, all three of these python libraries are thin wrappers over Graphviz, so none conceal the lightweight, elegant Graphviz syntax (the dotlanguage).

幸运的是,所有这三个 python 库都是Graphviz 的薄包装,所以没有一个隐藏轻量级、优雅的 Graphviz 语法(语言)。

alt text

替代文字

Here's the code (in graphviz' dotlanguage) I used to create the small "flowchart" below:

这是我用来创建下面的小“流程图”的代码(使用graphviz的语言):

digraph {

  node [    fill=cornflowerblue,
            fontcolor=white,
            shape=diamond,
            style=filled];

  Step1 [   color=darkgoldenrod2,
            fontcolor=navy,
            label=start,
            shape=box];

  Step2;

  Step3a [  style=filled,
            fillcolor=grey80,
            color=grey80,
            shape=circle,
            fontcolor=navy];

  Step1  -> Step2;
  Step1  -> Step2a;
  Step2a -> Step3a;
  Step3;
  Step3a -> Step3;
  Step3a -> Step2b;
  Step2  -> Step2b;
  Step2b -> Step3;
  End [ shape=rectangle,
        color=darkgoldenrod2,
        fontcolor=navy];
  Step3  -> End [label=193];
}

回答by Eric O Lebigot

Like doug, I would suggest Graphviz.

像道格一样,我建议使用 Graphviz。

I would also like to mention that you can also directly write graphs in the very simple dot language(they can then be plotted with Graphviz or other tools); this is a more lightweight alternative to using pydot, with no dependency of your code on any module.

我还想提一下,你也可以直接用非常简单的点语言写图(然后可以用 Graphviz 或其他工具绘制它们);这是使用 pydot 的更轻量级的替代方案,您的代码不依赖于任何模块。

回答by Noctis Skytower

gprof2dot.pycan automatically profile and visualize the execution flow in your program. It can be found as reciple 578138on ActiveState Code. Please note the batch file at the end of the program.

gprof2dot.py可以自动分析和可视化程序中的执行流程。它可以在 ActiveState Code 上作为reciple 578138找到。请注意程序末尾的批处理文件。