python graphviz的使用(画图工具)
一、graphviz安装及配置
graphviz实际上是一个绘图工具,可以根据dot脚本画出树形图等。
1、windows安装
- 安装graphviz软件:https://graphviz.gitlab.io/_pages/Download/Download_windows.html
- 配置环境变量:把bin文件夹的路径加入到环境变量path里
- 安装python的graphviz模块:pip install graphviz
2、linux centos7安装
- yum下载graphviz软件:yum -y install graphviz
- 安装python的graphviz模块:pip install graphviz
- 测试:which dot
二、graphviz的使用
graphviz 有两种图,一种是无向图 graph ,边用 -- 连接,一种是有向图 digraph ,边用 -> 连接
1、初步认识
from graphviz import Digraph # 实例化一个Digraph对象(有向图),name:生成的图片的图片名,format:生成的图片格式
dot = Digraph(name="MyPicture", comment="the test", format="png") # 生成图片节点,name:这个节点对象的名称,label:节点名,color:画节点的线的颜色
dot.node(name='a', label='Ming', color='green')
dot.node(name='b', label='Hong', color='yellow')
dot.node(name='c', label='Dong') # 在节点之间画线,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用在调试的时候
dot.render(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)
2、字体乱码
中文的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.node(name="A", label="老师", fontname="Microsoft YaHei")
dot.node('B', '学生', fontname="Microsoft YaHei")
dot.edge("A", "B", label="教学", fontname="Microsoft YaHei")
dot.render(filename="MyPicture")
3、无向图
用法跟有向图一样
from graphviz import Graph # 无向图
dot = Graph(name="MyPicture", format="png") dot.node("People")
dot.node("Home")
dot.edge("People", "Home")
dot.view(filename="MyPicture")
三、属性(样式)说明
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) |
例子
"""使用graph_attr, node_attr, edge_attr参数, 你可以更改图中节点和边的显示样式""" from graphviz import Digraph # 可以在实例化对象的时候设置样式
dot = Digraph(name="MyPicture", node_attr={"shape": "plaintext"}, format="png") # 也可以实例化之后, 设置这些样式
dot.graph_attr['rankdir'] = 'LR'
dot.edge_attr.update(arrowhead='vee', arrowsize='2') # 然后开始画图
dot.node("Dog")
dot.node("Cat")
dot.edge("Dog", "Cat") dot.view(filename="MyPicture")
四、官网
python graphviz的使用(画图工具)的更多相关文章
- Ubuntu 16.10 安装KolourPaint 4画图工具
KolourPaint 4画图工具简单实用,可以绘画.视频处理和图标编辑: • 绘画:绘制图表和“手绘” • 视频处理:编辑截图和照片;应用特效 • 图标编辑:绘画剪贴和标识透明化 1.在Ubuntu ...
- Python黑帽编程1.3 Python运行时与包管理工具
Python黑帽编程1.3 Python运行时与包管理工具 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and ...
- 用Canvas制作简单的画图工具
今天用Canvas制作了一个画图工具,非常简单,功能也不是很多,主要有背景网格,画线,画圆,画矩形和画圆角矩形,也用到了canvas的一些基本知识,在这里一一列举. 1.线段的绘制: 如何绘制真正的1 ...
- Flex 中画图工具(drawTool)失效
做项目的时候画图工具突然失效,解决了半天都不行,最后将画图结束的函数map_drawEndHandler写在方法里面的时候,运行却能够画图了,不知道是什么原理,比较头疼,左思右想,都感觉有点怪怪的,虽 ...
- 用Python编写博客导出工具
用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处 写在前面的话 我在 github 上用 octopress 搭建了个人博 ...
- 推荐ubuntu下的画图工具
今天发现ubuntu下面也有类似于windows画图的画图工具,功能也比较强大,支持各种格式的图片,也有各种工具,非常方便,安装的方法是: sudo apt-get install kolourpai ...
- DrawTools(画图工具)原始版本
上一篇文章一个优秀的C#开源绘图软件 DrawTools中详细的介绍了DrawTools的几种演化的较高版本的软件的特色与功能. 这篇文章,将介绍一下这款软件的成名版本, 下载地址DrawTool_O ...
- windows“画图”工具用法
图片编辑工具不少,photoshop功能强大,但是并不是那么容易掌握.而且,倘若一个简单的图片处理问题就使用这么强大的工具,有点“杀鸡焉用牛刀”的意思,要知道只打开photoshop就需要一段等待时间 ...
- HTML5简易在线画图工具
继上次学习了HTML5的路径画圆做了动态时钟.异次元空间的反转做了运动的太阳系,这两天将画线.画圆.填充等知识点结合起来做了一个简易的在线画图工具: 查看DEMO:HTML5简易在线画图工具 功能包括 ...
随机推荐
- history-back
;!function(pkg, undefined){ //此声明函数在xback.js文件里有,在app.js里必须再声明一次,不然监听返回事件失败 var STATE = 'x-back'; va ...
- IFIX 5.9 历史数据 曲线 (非SQL模式)
装完 ifix 5.9 默认是没有Hist 开头的 历史数据源的,没存,至少我装的版本是这样. 那个Historian 也没有安装包,好像还要授权,自己研究不了. 1 先把数据存本地 在你的安装包里 ...
- ESLint & vue
ESLint & vue { "name": "app", "version": "1.0.1", " ...
- H5 直播 & App 直播
H5 直播 & App 直播 polyv 直播 https://github.com/polyv 宝利威 直播 https://www.polyv.net/live/ SDK https:// ...
- Flutte 什么是Widget,RenderObjects和Elements
原文 Opacity API文档 有没有想过Flutter如何获取这些小部件并将其实际转换为屏幕上的像素? 您可能已经知道如何使用StatelessWidget和StatefulWidget.但是那些 ...
- Flutter Android Toast Message(flutter访问Android Toast Message)
原文 Android Toast通知可用于向用户发送快速消息,并在几秒钟后消失. 但是当涉及Flutter时,没有直接的方式来显示这些Toast消息.因此,我们需要找到一种替代方法来实现它.在这种情况 ...
- VAST维萨币二月发行,高倍币重现江湖!
市场长期的历史经验表明,经营盈利能力最好的企业,经常是那些现在的经营方式与5年前甚至10年前几乎完全相同的企业.这个经营模式放到币圈也是一样的,2020年的挖矿是最火的,这个模式现在在市场也同样受用. ...
- NGK底层技术如何助力SPC子币VAST高价与安全并行?
NGK近来使用了新的侧链技术推出了新的SPC侧链代币,以及SPC的子币VAST---维萨币. NGK使用去中心化和开源区块链数据分布式协议,不断打造高倍币,力求成为生态建设参与者们所信赖的高倍币孵化器 ...
- 人物传记-BILL RAY:低谷时的信念,决定你能走多远
自2018年全球经济危机以来,以工作为重的成年人们一直备受打击.尤其是2020年,全球贸易争端使得经济下滑严重,很多公司倒闭破产,有些人甚至从富豪变成了负债者,走向了人生低谷.其实,每个人都会遇到人生 ...
- C++算法代码——扫雷游戏
题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1685 题目描述 扫雷游戏是一款十分经典的单机小游戏. 在 n 行 m 列的雷区中有一 ...