文章目錄
-
- 一、Graphviz
-
- 1、關于 Graphviz
- 2、安裝
-
- 使用 brew 安裝
- 檢視版本
- 檢視指令幫助
- 3、使用
-
- 1) 建立檔案 g.dot
- 2) 編譯
- 二、pydotplus
-
- 1、關于 PyDotPlus
- 2、安裝
- 3、使用
一、Graphviz
1、關于 Graphviz
Graphviz 是一個繪制關系圖/流程圖的工具包,使用它提供的 dot-language ,可以在文本中通過 嵌入繪制代碼 的方式實作圖的繪制。
Graphviz 官網: http://www.graphviz.org
dot-language: https://graphviz.org/doc/info/lang.html
2、安裝
使用 brew 安裝
在 terminal 中輸入如下代碼進行下載下傳安裝
$ brew install graphviz
檢視版本
$ dot -V
dot - graphviz version 2.44.1 (20200629.0846)
檢視指令幫助
$ dot -h
Error: dot: option -h unrecognized
Usage: dot [-Vv?] [-(GNE)name=val] [-(KTlso)<val>] <dot files>
(additional options for neato) [-x] [-n<v>]
(additional options for fdp) [-L(gO)] [-L(nUCT)<val>]
(additional options for memtest) [-m<v>]
(additional options for config) [-cv]
-V - Print version and exit
-v - Enable verbose mode
-Gname=val - Set graph attribute 'name' to 'val'
-Nname=val - Set node attribute 'name' to 'val'
-Ename=val - Set edge attribute 'name' to 'val'
-Tv - Set output format to 'v'
-Kv - Set layout engine to 'v' (overrides default based on command name)
-lv - Use external library 'v'
-ofile - Write output to 'file'
-O - Automatically generate an output filename based on the input filename with a .'format' appended. (Causes all -ofile options to be ignored.)
-P - Internally generate a graph of the current plugins.
-q[l] - Set level of message suppression (=1)
-s[v] - Scale input by 'v' (=72)
-y - Invert y coordinate in output
-n[v] - No layout mode 'v' (=1)
-x - Reduce graph
-Lg - Don't use grid
-LO - Use old attractive force
-Ln<i> - Set number of iterations to i
-LU<i> - Set unscaled factor to i
-LC<v> - Set overlap expansion factor to v
-LT[*]<v> - Set temperature (temperature factor) to v
-m - Memory test (Observe no growth with top. Kill when done.)
-m[v] - Memory test - v iterations.
-c - Configure plugins (Writes $prefix/lib/graphviz/config
with available plugin information. Needs write privilege.)
-? - Print usage and exit
3、使用
參考:https://zhuanlan.zhihu.com/p/22820399
1) 建立檔案 g.dot
編寫代碼如下:
digraph{
1 -> 2;
2 -> 3;
3 -> main;
2 -> main;
m -> 2;
}
2) 編譯
$ dot -Tpng g.dot -o g.png
即可得到圖檔 g.png 如下:
二、pydotplus
1、關于 PyDotPlus
PyDotPlus : Python interface to Graphviz’s Dot language
位址:https://github.com/carlos-jenkins/pydotplus
2、安裝
(base) $ sudo pip install pydotplus
3、使用
實作和上方同樣的效果
import pydotplus
from IPython.display import Image
dot_content = 'digraph{ \
1 -> 2; \
2 -> 3; \
3 -> main; \
2 -> main; \
m -> 2;\
}'
graph = pydotplus.graph_from_dot_data(dot_content)
graph.write_png('a.png') # 寫入到檔案
Image(graph.create_png())
更多樣式
參考:https://www.cnblogs.com/qizhou/p/12743708.html
pydotplus是别的語言嫁接到python裡面的,是以繪制要傳入
字元串
形式表示的結構,而沒有python的結構對象直接用來畫。
字元串的形式有點像C中的結構體,但是可以不寫分号,用空格代替也可将語句分開。
在“結構體”中,可以設定點與邊的樣式和字型,并且可以給邊添加label。
import pydotplus
from IPython.display import Image
dot_content = 'digraph{ \
1 -> 2[label="很好"]; \
m -> 2[label="一般"];\
2 -> 3; \
3 -> main; \
2 -> main; \
1[label="周五"]\
2[label="明天不上班", color="red"]\
main[label="周日晴天"]\
}'
graph = pydotplus.graph_from_dot_data(dot_content)
# graph.get_nodes().set_fillcolor("#FFF2DD")
Image(graph.create_png())
本文基于 macOS 10.15.6
2020-11-04 伊織