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

发布于 2024-10-07 22:31:03 字数 1536 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑白记忆 2024-10-14 22:31:03

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

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

Graphviz 不是一个 Python 库(它是用 C 编写的);然而,有高质量的 python 绑定可用。

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

另外两个是 pydotyapgvb。我至少用过这两种方法几次。每个都比 pygraphviz 小(这可能是一个优势,具体取决于您的用例);此外,两者都没有像 pygraphviz 那样记录。

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

alt text

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

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];
}

Graphviz is the best option in my opinion.

Graphviz is 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 constraints on 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 is not a python library (it's written in C); however there are high quality python bindings available.

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

The other two are pydot and 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.

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

alt text

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

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];
}
关于从前 2024-10-14 22:31:03

和 doug 一样,我建议使用 Graphviz。

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

Like doug, I would suggest 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.

温柔戏命师 2024-10-14 22:31:03

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文