分类目录——万能的Python系列

近来发现了一个神奇的画流程图的工具——Graphviz

Graphviz官网

什么是Graphviz?

Graphviz是开源的图形可视化软件。图形可视化是一种将结构信息表示为抽象图形和网络图的方式。它在网络,生物信息学,软件工程,数据库和网页设计,机器学习以及其他技术领域的可视界面中具有重要的应用。

python画流程图的库 python流程图工具_graphviz

引自 Graphviz官网+谷歌翻译

就是一个应用性很好的画流程图工具

Graphviz支持在文本文档中写语法命令行绘图、支持在Markdown文档中绘图、也有相应的Python工具包通过编程绘图

使用之前首先要在本地安装,Windows下在 下载地址 下载压缩包解压即可使用

解压之后为了方便使用需要配置环境变量,将工具文件中的bin目录(如我的是 C:\Program Files\graphviz\bin)添加到环境变量中,此时在命令行中就可以直接调用其命令了,但是我配置完环境变量紧接着在Pycharm中操作时新配置的环境变量并没有生效,大概是需要重启电脑的,我是在Pycharm中通过os模块的命令又添加了一下环境变量

  • Python编程绘图
  • 先看效果
  • 导入支持包
from graphviz import Digraph
from graphviz import Source
  • 因为环境变量不能即时生效配置环境变量
import os
os.environ["PATH"] += os.pathsep + r'C:\Program Files\graphviz\bin'
  • 创建图对象
dot = Digraph(
    name='Graphtest',
    comment='添加到源码第一行的注释',
    filename=None,
    directory=None,
    format="png",
    engine=None,
    encoding='utf8',
    graph_attr={'rankdir':'TB'},
    node_attr={'color':'black','fontcolor':'black','fontname':'FangSong','fontsize':'12','style':'rounded','shape':'box'},
    edge_attr={'color':'#999999','fontcolor':'#888888','fontsize':'10','fontname':'FangSong'},
    body=None,
    strict=False
)

其中

  • name: 图的名字,打开时显示的图的名字.
  • comment: 添加的源码第一行的注释.
  • filename: 指定保存图片时的文件名
  • directory: (Sub)directory for source saving and rendering.
  • format: 输出图片的格式 ('pdf', 'png', …).
  • engine: Layout command used ('dot', 'neato', …).
  • encoding: 图的编码方式,such as ‘utf8’.
  • graph_attr: 图属性,属性字典的形式.
  • node_attr: 节点属性,属性字典的形式.
    shape可以是oval(椭圆)、circle(圆)、box(圆角矩形)。。。
  • edge_attr: 边(连线)属性,属性字典的形式.
  • body: Iterable of verbatim lines to add to the graph body.
  • strict (bool): 如果设置了多条A->B,渲染时多条合并.

其中node_attr、edge_attr中有fontname这样一个属性,用来指定字体,尤其绘图中有中文的时候需要指定一个支持中文的字体(默认是不支持中文的)

  • 添加节点——dot.node()
dot.node('A', 'this is A', {'shape':'circle','color':'blue','fontcolor':'blue'})
# shape 节点形状
# color 颜色
# fontcolor 字体颜色
dot.node('B', 'this is B')
dot.node('C', 'this is C')
dot.node('D', 'this is D')
dot.node('E', 'this is E')
dot.node('F', 'this is F')

在声明Digraph()对象时也进行节点属性的的指定,此时以这里为准,就近原则

  • 添加边——dot.edge() dot.edges()
dot.edge('A', 'B', 'test', _attributes={'style':'dotted', 'dir':'both'})
# style 线的类型,实线,虚线等
# dir   线(箭头)的方向,单向、双向等

# 创建一堆边,即连接AB的两条边,连接AC的一条边。
dot.edges(['AC', 'BD', 'BE', 'EF'])

在声明Digraph()对象时也进行边属性的的指定,此时以这里为准,就近原则

  • dot.其他
dot.view()
显示图,注意语句的位置,只显示该句之前声明的图元素

dot.source
获得该图对应的文本语法(可以直接拷贝到Markdown中显示,直接保存到文本文档中可以调用命令行绘图)

dot.save(filename='source.gv', directory='data')
# 保存源码,可以指定文件名,文件名取 指定名>Digraph属性中的filename>Digraph属性中的name

dot.render(directory='data') 
# 保存图片,可以指定文件名,文件名取 指定名>filename>name

# 从保存的文件读取并显示
dot_ = Source.from_file('data/source.gv')
print(dot_.source)  # 打印文本如法如下
# // 添加到源码第一行的注释
# digraph Graphtest {
# 	graph [rankdir=TB]
# 	node [color=black fontcolor=black fontname=FangSong fontsize=12 shape=box style=rounded]
# 	edge [color="#999999" fontcolor="#888888" fontname=FangSong fontsize=10]
# 	A [label="this is A" color=blue fontcolor=blue shape=circle]
# 	B [label="this is B"]
# 	C [label="this is C"]
# 	D [label="this is D"]
# 	E [label="this is E"]
# 	F [label="this is F"]
# 	A -> B [label=test dir=both style=dotted]
# 	A -> C
# 	B -> D
# 	B -> E
# 	E -> F
# }

dot.view()效果如本节开头所示

  • 在Markdown文档中
    语法
![testgraphviz](https://g.gravizo.com/svg?流程图的定义语句)

一个例子,取自 https://graphviztutorial.readthedocs.io/zh_CN/latest/chap01.html#id3

![testgraphviz](https://g.gravizo.com/svg?
digraph G {
    main [shape=box];
    main -> parse [weight=8];
    parse-> execute;
    main -> init [style=dotted];
    main -> cleanup;
    execute -> {make_string, printf};
    init -> make_string;
    edge [color=red];
    main -> printf [style=bold, label="100 times"];
    make_string [label = "make a\nstring"];
    node [shape=box, style=filled,color=".7, .3, 1.0"];
    execute -> compare;
})

跟多绘图语法可以搜索关键字 DOT语法

效果

![testgraphviz](https://g.gravizo.com/svg?
 digraph G {
 main [shape=box];
 main -> parse [weight=8];
 parse-> execute;
 main -> init [style=dotted];
 main -> cleanup;
 execute -> {make_string, printf};
 init -> make_string;
 edge [color=red];
 main -> printf [style=bold, label=“100 times”];
 make_string [label = “make a\nstring”];
 node [shape=box, style=filled,color=".7, .3, 1.0"];
 execute -> compare;
 })
  • 文本文档+命令行
    参见 Graphviz Tutorial 1.0 文档,注意文件的后缀名要正确指定
    跟多绘图语法可以搜索关键字 DOT语法

说明比较简陋,仅抛砖引玉

参考文献

Graphviz Tutorial 1.0 文档

Node, Edge and Graph Attributes