pythongraphviz的使⽤(画图⼯具)
graphviz实际上是⼀个绘图⼯具,可以根据dot脚本画出树形图等。
1. 安装graphviz软件:
2. 配置环境变量:把bin⽂件夹的路径加⼊到环境变量path⾥
3. 安装python的graphviz模块:pip install graphviz
1. yum下载graphviz软件:yum -y install graphviz
2. 安装python的graphviz模块:pip install graphviz
3. 测试:which dot
graphviz 有两种图,⼀种是⽆向图graph,边⽤--连接,⼀种是有向图digraph,边⽤->连接
from graphviz import Digraph
# 实例化⼀个Digraph对象(有向图),name:⽣成的图⽚的图⽚名,format:⽣成的图⽚格式
dot = Digraph(name="MyPicture", comment="the test", format="png")
# ⽣成图⽚节点,name:这个节点对象的名称,label:节点名,color:画节点的线的颜⾊
# 在节点之间画线,label:线上显⽰的⽂本,color:线的颜⾊
dot.edge('a', 'b', label="ab\na-b", color='red')
# ⼀次性画多条线,c到b的线,a到c的线
dot.edges(['cb', 'ac'])
# 打印⽣成的源代码
print(dot.source)
# 画图,filename:图⽚的名称,若⽆filename,则使⽤Digraph对象的name,默认会有gv后缀
# directory:图⽚保存的路径,默认是在当前路径下保存
dot.view(filename="mypicture", directory="D:\MyTest")
# 跟view⼀样的⽤法(render跟view选择⼀个即可),⼀般⽤render⽣成图⽚,不使⽤view=True,view=True⽤在调试的时候der(filename='MyPicture', directory="D:\MyTest",view=True)
使⽤node()和edge()或edges()⽅法将节点和边添加到图形对象:
Digraph():实例化⼀个图形对象
node():⽅法第⼀个参数是name,第⼆个参数是label,即node画节点
edges():⽅法可以⼀次添加多个边, 每个边⽤字符串表⽰, ⽐如cb 表⽰从 c 到 b 的边,即edges画边
edge():⽅法⼀次添加⼀个边
view():把图形画出来,并⾃动显⽰图⽚(弹出来),⼀般使⽤view()进⾏调试
render():把图形画出来,⼀般使⽤render保存图⽚的时候,view=False(不弹出图⽚)
调试推荐使⽤ view()
保存图⽚推荐使⽤ render(view=False)
中⽂的label默认是⽆法正确显⽰在图中的, 因为默认的字体并不⽀持中⽂, 需要我们为node设置字体。
# 有些字体是需要下载的,默认使⽤Microsoft YaHei就好
0、字体样式微软雅⿊:Microsoft YaHei
1、字体样式华⽂⿊体:STHeiti
2、字体样式华⽂楷体:STKaiti
3、字体样式华⽂宋体:STSong
4、字体样式华⽂仿宋:STFangsong
5、字体样式⿊体:SimHei
6、字体样式宋体:SimSun
7、字体样式新宋体:NSimSun
8、字体样式仿宋:FangSong
9、字体样式楷体:KaiTi
10、字体样式仿宋_GB2312:FangSong_GB2312
11、字体样式楷体_GB2312:KaiTi_GB2312
12、字体样式微软正⿊体:Microsoft JhengHei
13、字体样式微软雅⿊体:Microsoft YaHei
14、字体样式⾪书:LiSu
15、字体样式幼圆:YouYuan
16、字体样式华⽂细⿊:STXihei
17、字体样式华⽂楷体:STKaiti
18、字体样式华⽂宋体:STSong
19、字体样式华⽂中宋:STZhongsong
20、字体样式华⽂仿宋:STFangsong
21、字体样式⽅正舒体:FZShuTi
22、字体样式⽅正姚体:FZYaoti
23、字体样式华⽂彩云:STCaiyun
24、字体样式华⽂琥珀:STHupo
25、字体样式华⽂⾪书:STLiti
26、字体样式华⽂⾏楷:STXingkai
27、字体样式华⽂新魏:STXinwei
字体样式
from graphviz import Digraph
dot = Digraph(name="MyPicture", format="png")
dot.edge("A", "B", label="教学", fontname="Microsoft YaHei")
⽤法跟有向图⼀样
from graphviz import Graph
# ⽆向图
dot = Graph(name="MyPicture", format="png")
dot.edge("People", "Home")
dot.view(filename="MyPicture")
Name Default Values
color black node shape color
comment any string (format-dependent)
distortion0.0node distortion for shape=polygon
fillcolor lightgrey/black node fill color
fixedsize false label text has no affect on node size fontcolor black type face color
fontname Times-Roman font family
fontsize14point size of label
group name of node’s group
height.5height in inches
label node name any string
layer overlay range all, id or id:id
orientation0.0node rotation angle
peripheries shape-dependent number of node boundaries
regular false force polygon to be regular
shape ellipse node shape; see Section 2.1 and Appendix E shapefile external EPSF or SVG custom shape file sides4number of sides for shape=polygon
skew0.0skewing of node for shape=polygon
style graphics options, e.g. bold, dotted, filled; cf. Section 2.3
URL URL associated with node (format-dependent) width.75width in inches
z0.0z coordinate for VRML output
Name Default Values arrowhead normal style of arrowhead at head end arrowsize  1.0scaling factor for arrowheads
arrowtail normal style of arrowhead at tail end
color black edge stroke color comment
any string (format-dependent)constraint
true use edge to affect node ranking decorate
if set, draws a line connecting labels with their edges dir
forward forward, back, both, or none fontcolor
black type face color fontname
Times-Roman font family fontsize
14point size of label headlabel
label placed near head of edge headport
n,ne,e,se,s,sw,w,nw headURL
URL attached to head label if output format is ismap label
edge label labelangle
-25.0angle in degrees which head or tail label is rotated off edge labeldistance
1.0scaling factor for distance of head or tail label from node labelfloat
false lessen constraints on edge label placement labelfontcolor
black type face color for head and tail labels labelfontname
Times-Roman font family for head and tail labels labelfontsize
14point size for head and tail labels layer
overlay range all, id or id:id lhead
name of cluster to use as head of edge ltail
name of cluster to use as tail of edge minlen
1minimum rank distance between head and tail samehead
tag for head node; edge heads with the same tag are sametail
merged onto the same port style
tag for tail node; edge tails with the same tag are merged onto the same port taillabel
graphics options, e.g. bold, dotted, filled; cf. Section 2.3tailport
label placed near tail of edge n,ne,e,se,s,sw,w,nw tailURL
URL attached to tail label if output format is ismap weight
1integer cost of stretching an edge
Name Default Values
Name
Default Values bgcolor
background color for drawing, plus initial fill color center
false center drawing on page clusterrank
local may be global or none color
black for clusters, outline color, and fill color if fillcolor not defined comment
any string (format-dependent)compound
false allow edges between clusters concentrate
false enables edge concentrators fillcolor
black cluster fill color fontcolor
black type face color fontname
Times-Roman font family fontpath
list of directories to search for fonts fontsize
14point size of label label
python在线工具菜鸟工具
any string labeljust
centered ”l” and ”r” for left- and right-justified cluster labels, respectively labelloc
top ”t” and ”b” for top- and bottom-justified cluster labels, respectively layers
id:id:id…margin
.5margin included in page, inches mclimit
1.0scale factor for mincross iterations nodesep
.25separation between nodes, in inches.nslimit
if set to f, bounds network simplex iterations by (f)(number of nodes) when setting x-coordinates nslimit1
if set to f, bounds network simplex iterations by (f)(number of nodes) when ranking nodes ordering
if out out edge order is preserved orientation
portrait if rotate is not used and the value is landscape, use landscape orientation page  unit of pagination, e.g. “8.5,11”
pagedir BL traversal order of pages quantum  if quantum ¿ 0.0, node label dimensions will be rounded to integral multiples of quantum rank
same, min, max, source or sink rankdir
TB LR (left to right) or TB (top to bottom)ranksep
.75separation between ranks, in inches.ratio
approximate aspect ratio desired, fill or auto remincross
if true and there are multiple clusters, re-run crossing minimization rotate
If 90, set orientation to landscape samplepoints
8number of points used to represent ellipses and circles on output (cf. Appendix C searchsize
30maximum edges with negative cut values to check when looking for a minimum one during network simplex size
maximum drawing size, in inches style
graphics options, e.g. filled for clusters URL
URL associated with graph (format-dependent)
Name
Default Values
"""使⽤graph_attr, node_attr, edge_attr 参数, 你可以更改图中节点和边的显⽰样式"""from  graphviz import  Digraph
# 可以在实例化对象的时候设置样式
dot = Digraph(name="MyPicture", node_attr={"shape": "plaintext"}, format="png")# 也可以实例化之后, 设置这些样式
dot.edge_attr.update(arrowhead ='vee', arrowsize='2')
# 然后开始画图
dot.edge("Dog", "Cat")
dot.view(filename ="MyPicture")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。