graphviz的基本语法及使用

简介: graphviz的基本语法及使用

基本语法

字符串 都要加双引号, 可以加\n换行符

注释 双斜杠// 或/* */

有向图 digraph, 节点关系: 指向->

无向图 graph, 节点关系: 连通 --

属性 node[attribute1=value1, attribute2=value2]

大小: size=”2,2”; 单位为英寸

标签: label=”显示在图上的内容”

边:edge [color=red,style=dotted]; 这句话之后生效

节点:node [color=navy]; 这句话之后生效

边方向:rankdir=参数值;LR(从左到右),RL,BT,TB

节点形状: a[shape=box]; 默认是椭圆

边框大小:a[width=.1,height=2.5]; 单位为英寸

边框颜色:a[color=red];

构造边

关系

有向图

无向图

一对一

a->b;

a–b;

一对多

a->{b;c;d};

a–{b;c;d};

多对一

{b;c;d}->a;

{b;c;d}–a;

多对多

{m,n,p,q}->{b;c;d};

{m,n,p,q}->{b;c;d};

详细资料:

官方文档:http://www.graphviz.org/documentation/

属性设置:https://graphviz.gitlab.io/_pages/doc/info/attrs.html

节点形状:https://graphviz.gitlab.io/_pages/doc/info/shapes.html

箭头形状:https://graphviz.gitlab.io/_pages/doc/info/arrows.html

颜色配置:https://graphviz.gitlab.io/_pages/doc/info/colors.html


基本图形绘制

digraph G {
    A -> B;
    A -> C -> B;
    A -> D -> B;
}

图片

a27.2.png


节点形状

digraph G {
    size = "4, 4";
    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\n字符串"];
    node [shape=box,style=filled,color=".7 .3 1.0"];
    execute -> compare;
}

a27.3.png


标签

digraph G {
    a -> b -> c;
    b -> d;
    a [shape=polygon,sides=5,peripheries=3,color=lightblue,style=filled];
    c [shape=polygon,sides=4,skew=.4,label="hello world"]
    d [shape=invtriangle];
    e [shape=polygon,sides=4,distortion=.7];
}

a27.4.png


类似HTML的样式

 digraph html {
    abc [shape=none, margin=0, label=<
    <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
        <TR>
            <TD ROWSPAN="3"><FONT COLOR="red">hello</FONT><BR/>world</TD>
            <TD COLSPAN="3">b</TD>
            <TD ROWSPAN="3" BGCOLOR="lightgrey">g</TD>
            <TD ROWSPAN="3">h</TD>
        </TR>
        <TR>
            <TD>c</TD>
            <TD PORT="here">d</TD>
            <TD>e</TD>
        </TR>
        <TR>
            <TD COLSPAN="3">f</TD>
        </TR>
    </TABLE>>];
}

a27.5.png

二分查找树

digraph g {
    node [shape = record,height=.1];
    node0[label = "<f0> |<f1> G|<f2> "];
    node1[label = "<f0> |<f1> E|<f2> "];
    node2[label = "<f0> |<f1> B|<f2> "];
    node3[label = "<f0> |<f1> F|<f2> "];
    node4[label = "<f0> |<f1> R|<f2> "];
    node5[label = "<f0> |<f1> H|<f2> "];
    node6[label = "<f0> |<f1> Y|<f2> "];
    node7[label = "<f0> |<f1> A|<f2> "];
    node8[label = "<f0> |<f1> C|<f2> "];
    "node0":f2 -> "node4":f1;
    "node0":f0 -> "node1":f1;
    "node1":f0 -> "node2":f1;
    "node1":f2 -> "node3":f1;
    "node2":f2 -> "node8":f1;
    "node2":f0 -> "node7":f1;
    "node4":f2 -> "node6":f1;
    "node4":f0 -> "node5":f1;
}

a27.6.png


示例

//定义节点属性
digraph g {
    //==========定义节点关系============
    a->b;
    b->c;
    c->a;
    c->d->e->f;
    d->g;
    e->h;
    //==========定义节点属性============
    //定义a节点为长方形, 样式为填充, 填充颜色为#ABACBA
    a[shape=box,label="Server1\nWebServer",fillcolor="#ABACBA",style=filled];
    //定义b为5边形, 标签为"bb", 样式为填充, 填充色为red
    b[shape=polygon,sides=5,label="bb",style=filled,fillcolor=red];
    //c, 默认为椭圆
    d[shape=circle]; //园
    e[shape=triangle]; //三角形
    f[shape=polygon, sides=4, skew=0.5]; //平行四边形
    g[shape=polygon, distortion=0.5]; //梯形, 上边长
    h[shape=polygon, distortion=-.5]; //梯形, 下边长
}

a27.8.png


有向图

digraph g {
  //edge[style=dashed]; //定义边的样式, 虚线
  node[peripheries=2, style=filled, color="#eecc80"];
  a->b [color=red, style=dashed]; //定义边的颜色, 红色 (b和方括号之间必须有空格)
  b->c; //箭头, 三角形; 箭尾, 菱形
  b->d [arrowhead=box]; //箭头, 长方形
  b->e [dir=none]; //没有箭头
  d->f [dir=both]; //双向箭头
  f->h [label=go]; //定义edge的标签
  f->k [arrowhead=diamond]; //更改箭头形状
  k->y [headlabel="哈哈", taillabel="洗洗"];
}

a27.9.png


// (更多箭头形状请参考官方文档: http://www.graphviz.org/content/arrow-shapes)

无向图

graph g {
  edge[style=dashed]; //定义边的样式, 虚线
  a -- b [color=red]; //定义边的颜色, 红色 (b和方括号之间必须有空格)
}

a27.10.png


参考:

  1. 官网文档:https://graphviz.gitlab.io/_pages/pdf/dotguide.pdf
  2. graphviz dot语言学习笔记
  3. dot语法
相关文章
|
Python
DataFrame排序和排名案例解析
本文演示了如何使用pandas对DataFrame进行排序和排名。首先,通过`pd.DataFrame()`将字典转换为DataFrame,然后利用`sort_values()`按&#39;年龄&#39;列进行升序排序。此外,还使用`rank()`方法为&#39;年龄&#39;列生成排名,并将其添加到DataFrame中作为新的&#39;年龄排名&#39;列。
384 0
|
5月前
|
人工智能 搜索推荐 安全
从库存优化到GMV增长:淘宝API如何赋能电商企业降本增效?
淘宝API分类全解析:从商品管理、订单处理到智能营销,系统梳理其接口生态与应用场景,助力电商技术选型与业务升级。
|
固态存储
计算机硬件更换硬件
【7月更文挑战第27天】
856 6
|
负载均衡 关系型数据库 数据管理
关系型数据库的横向扩展
【5月更文挑战第2天】关系型数据库的横向扩展
448 6
关系型数据库的横向扩展
qt中编译错误:error: C2001: 常量中有换行符的解决办法
qt中编译错误:error: C2001: 常量中有换行符的解决办法
|
JavaScript
Selenium--WebDriverWait--你知道显示等待?(结合源码让你更加得心应手)
Selenium--WebDriverWait--你知道显示等待?(结合源码让你更加得心应手)
329 0
|
网络协议 C++ Docker
Docker pull拉取镜像报错“Error response from daemon: Get "https://registry-1.docker.io/v2”解决办法
Docker pull拉取镜像报错“Error response from daemon: Get "https://registry-1.docker.io/v2”解决办法
63898 2
|
存储 Java API
Spring揭秘:Environment接口应用场景及实现原理!
Environment接口提供了强大且灵活的环境属性管理能力,通过它,开发者能轻松地访问和配置应用程序运行时的各种属性,如系统属性、环境变量等。 同时,Environment接口还支持属性源的定制和扩展,使得开发者能根据实际需求灵活地定制属性的加载和解析方式。
435 1
Spring揭秘:Environment接口应用场景及实现原理!
|
前端开发 JavaScript 开发者
NPM简介与使用指南:打造前端开发的利器
NPM简介与使用指南:打造前端开发的利器
482 0
|
机器学习/深度学习 算法 数据挖掘
实战Scikit-Learn:处理不平衡数据集的策略
【4月更文挑战第17天】本文探讨了Scikit-Learn处理不平衡数据集的策略,包括重采样(过采样少数类如SMOTE,欠采样多数类如RandomUnderSampler)、修改损失函数(如加权损失函数)、使用集成学习(如随机森林、AdaBoost)以及选择合适的评估指标(精确率、召回率、F1分数)。这些方法有助于提升模型对少数类的预测性能和泛化能力。
1212 1