node 节点属性如下 :
Name |
Default |
Values |
color |
black |
node shape color |
comment |
|
any string (format-dependent) |
distortion |
0.0 |
node 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 |
fontsize |
14 |
point size of label |
group |
|
name of node’s group |
height |
.5 |
height in inches |
label |
node name |
any string |
layer |
overlay range |
all, id or id:id |
orientation |
0.0 |
node 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 |
sides |
4 |
number of sides for shape=polygon |
skew |
0.0 |
skewing 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 |
.75 |
width in inches |
z |
0.0 |
z coordinate for VRML output |
edge 边框属性如下:
Name |
Default |
Values |
arrowhead |
normal |
style of arrowhead at head end |
arrowsize |
1.0 |
scaling 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 |
14 |
point 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.0 |
angle in degrees which head or tail label is rotated off edge |
labeldistance |
1.0 |
scaling 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 |
14 |
point 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 |
1 |
minimum 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.3 |
tailport |
|
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 |
1 |
integer cost of stretching an edge |
Digraph 图属性如下:
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 |
14 |
point size of label |
label |
|
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 |
.5 |
margin included in page, inches |
mclimit |
1.0 |
scale factor for mincross iterations |
nodesep |
.25 |
separation 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 |
.75 |
separation 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 |
8 |
number of points used to represent ellipses and circles on output (cf. Appendix C |
searchsize |
30 |
maximum 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) |
from graphviz import Graph
from graphviz import Digraph
# 绘制无向图
def draw_Graph():
# 创建对象,设置对象属性
dot = Graph(name='无向图', filename=r'D:\Cache\python\无向图', format='png')
# 设置节点属性, 修改 fontname 属性可解决中文输出乱码问题
dot.node_attr.update(shape='ellipse', color='black', fillcolor='lightgrey', fontcolor='black', fontsize='18',
fontname='Microsoft YaHei', width='4', height='2', style='bold')
# 创建节点, 设置节点名字、内容
dot.node('A', '小朋友')
dot.node('B', '小学生')
dot.node('C', '初中生')
dot.node('D', '高中生')
dot.node('E', '大学生')
dot.node('F', '研究生')
dot.node('G', '博士生')
dot.node('H', '医生')
dot.node('I', '老师')
dot.node('J', '程序员')
dot.node('K', '会计')
dot.node('L', '公务员')
dot.node('M', '教授')
# 设置边属性
dot.edge_attr.update(color='black', fontcolor='black', fontsize='18', fontname='Microsoft YaHei')
# 创建单边,设置边内容
dot.edge('A', 'B', '升级')
dot.edge('B', 'C', '升级')
dot.edge('C', 'D', '升级')
dot.edge('D', 'E', '升级')
dot.edge('E', 'F', '升级')
dot.edge('F', 'G', '升级')
# 创建多边,不能设置边内容
dot.edges(['EI', 'FI', 'GI', 'FH', 'GH', 'EJ', 'FJ', 'GJ', 'EK', 'FK', 'GK', 'EL', 'FL', 'GL', 'GM'])
# 打印生成的源代码,pdf 格式无法打印
print(dot.source)
# 保存图片,不显示图片
dot.render(filename='无向图', directory=r'D:\Cache\python')
# 保存图片,并显示图片
dot.view(filename='无向图', directory=r'D:\Cache\python')
# 绘制有向图
def draw_Digraph():
# 创建对象,设置对象属性
dot = Digraph(name='有向图', filename=r'D:\Cache\python\有向图', format='pdf')
# 设置节点属性, 修改 fontname 属性可解决中文输出乱码问题
dot.node_attr.update(shape='ellipse', color='black', fillcolor='lightgrey', fontcolor='black', fontsize='18',
fontname='Microsoft YaHei', width='4', height='2', style='bold')
# 创建节点, 设置节点名字、内容
dot.node('A', '小朋友')
dot.node('B', '小学生')
dot.node('C', '初中生')
dot.node('D', '高中生')
dot.node('E', '大学生')
dot.node('F', '研究生')
dot.node('G', '博士生')
dot.node('H', '医生')
dot.node('I', '老师')
dot.node('J', '程序员')
dot.node('K', '会计')
dot.node('L', '公务员')
dot.node('M', '教授')
# 设置边属性, arrowhead 为前端箭头样式, arrowsize 为箭头缩放比例, minlen 为边头部和尾部的距离, pnewidth 为边宽度
dot.edge_attr.update(color='black', fontcolor='black', fontsize='18', fontname='Microsoft YaHei',
arrowhead='diamond', arrowsize='2.0', minlen='3', penwidth='2', style='dashed')
# 创建单边,设置边内容
dot.edge('A', 'B', '升级')
dot.edge('B', 'C', '升级')
dot.edge('C', 'D', '升级')
dot.edge('D', 'E', '升级')
dot.edge('E', 'F', '升级')
dot.edge('F', 'G', '升级')
# 创建多边,不能设置边内容
dot.edges(['EI', 'FI', 'GI', 'FH', 'GH', 'EJ', 'FJ', 'GJ', 'EK', 'FK', 'GK', 'EL', 'FL', 'GL', 'GM'])
# 保存图片,不显示图片
dot.render(filename='有向图', directory=r'D:\Cache\python')
# 保存图片,并显示图片
dot.view(filename='有向图', directory=r'D:\Cache\python')
if __name__ == '__main__':
draw_Graph()