老男孩爱篮球 發表於 2019-9-1 13:33:00

python graphviz的使用(画图工具)

<p>参考文章1<br></p>
<p>参考文章2</p>
<h3>一、graphviz安装及配置</h3>
<p>graphviz实际上是一个绘图工具,可以根据dot脚本画出树形图等。</p>
<p>&nbsp;</p>
<h4>1、windows安装</h4>
<ol>
<li>安装graphviz软件:https://graphviz.gitlab.io/_pages/Download/Download_windows.html</li>
<li>配置环境变量:把bin文件夹的路径加入到环境变量path里</li>
<li>安装python的graphviz模块:pip <span class="hljs-keyword">install graphviz</span></li>







</ol>
<p>&nbsp;</p>
<h4>2、linux centos7安装</h4>
<ol>
<li>yum下载graphviz软件:yum -y install graphviz</li>
<li>安装python的graphviz模块:pip install graphviz</li>
<li>测试:which dot</li>







</ol>
<p>&nbsp;</p>
<h3>二、graphviz的使用</h3>
<p><span style="color: rgba(153, 51, 0, 1)"><strong>graphviz 有两种图,一种是无向图&nbsp;<code>graph</code>&nbsp;,边用&nbsp;<code>--</code>&nbsp;连接,一种是有向图&nbsp;<code>digraph</code>&nbsp;,边用&nbsp;<code>-&gt;</code>&nbsp;连接</strong></span></p>
<h4>1、初步认识</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> graphviz <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Digraph

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实例化一个Digraph对象(有向图),name:生成的图片的图片名,format:生成的图片格式</span>
dot = Digraph(name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyPicture</span><span style="color: rgba(128, 0, 0, 1)">"</span>, comment=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">the test</span><span style="color: rgba(128, 0, 0, 1)">"</span>, format=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">png</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 生成图片节点,name:这个节点对象的名称,label:节点名,color:画节点的线的颜色</span>
dot.node(name=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>, label=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Ming</span><span style="color: rgba(128, 0, 0, 1)">'</span>, color=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">green</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
dot.node(name</span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">b</span><span style="color: rgba(128, 0, 0, 1)">'</span>, label=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Hong</span><span style="color: rgba(128, 0, 0, 1)">'</span>, color=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">yellow</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
dot.node(name</span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">c</span><span style="color: rgba(128, 0, 0, 1)">'</span>, label=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Dong</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 在节点之间画线,label:线上显示的文本,color:线的颜色</span>
dot.edge(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">b</span><span style="color: rgba(128, 0, 0, 1)">'</span>, label=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ab\na-b</span><span style="color: rgba(128, 0, 0, 1)">"</span>, color=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">red</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 一次性画多条线,c到b的线,a到c的线</span>
dot.edges([<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">cb</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">ac</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">])

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 打印生成的源代码</span>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(dot.source)

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 画图,filename:图片的名称,若无filename,则使用Digraph对象的name,默认会有gv后缀</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> directory:图片保存的路径,默认是在当前路径下保存</span>
dot.view(filename=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mypicture</span><span style="color: rgba(128, 0, 0, 1)">"</span>, directory=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D:\MyTest</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 跟view一样的用法(render跟view选择一个即可),一般用render生成图片,不使用view=True,view=True用在调试的时候</span>
dot.render(filename=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">MyPicture</span><span style="color: rgba(128, 0, 0, 1)">'</span>, directory=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">D:\MyTest</span><span style="color: rgba(128, 0, 0, 1)">"</span>,view=True)</pre>
</div>
<p>使用node()和edge()或edges()方法将节点和边添加到图形对象:</p>
<ul>
<li><span style="font-family: monospace">Digraph():实例化一个图形对象</span></li>
<li>node():方法第一个参数是name,第二个参数是label,即node画节点</li>
<li><code>edges():</code>方法可以一次添加多个边, 每个边用字符串表示, 比如&nbsp;<code>cb&nbsp;</code>表示从 c 到 b 的边,即edges画边</li>
<li><code>edge():</code>方法一次添加一个边</li>
<li>view():把图形画出来,并自动显示图片(弹出来),一般使用view()进行调试</li>
<li>render():把图形画出来,一般使用render保存图片的时候,view=False(不弹出图片)</li>
</ul>
<p><strong><span style="color: rgba(153, 51, 0, 1)">调试推荐使用 view()</span></strong></p>
<p><strong><span style="color: rgba(153, 51, 0, 1)">保存图片推荐使用 render(view=False)</span></strong></p>
<p>&nbsp;</p>
<h4>2、字体乱码</h4>
<p>中文的label默认是无法正确显示在图中的, 因为默认的字体并不支持中文, 需要我们为node设置字体。</p>
<div class="cnblogs_code"><img id="code_img_closed_ae3190fa-c376-4604-94b5-36cde03bab97" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_ae3190fa-c376-4604-94b5-36cde03bab97" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_ae3190fa-c376-4604-94b5-36cde03bab97" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 有些字体是需要下载的,默认使用Microsoft YaHei就好</span>
<span style="color: rgba(0, 0, 0, 1)">
0、字体样式微软雅黑:Microsoft YaHei

</span>1<span style="color: rgba(0, 0, 0, 1)">、字体样式华文黑体:STHeiti

</span>2<span style="color: rgba(0, 0, 0, 1)">、字体样式华文楷体:STKaiti

</span>3<span style="color: rgba(0, 0, 0, 1)">、字体样式华文宋体:STSong

</span>4<span style="color: rgba(0, 0, 0, 1)">、字体样式华文仿宋:STFangsong

</span>5<span style="color: rgba(0, 0, 0, 1)">、字体样式黑体:SimHei

</span>6<span style="color: rgba(0, 0, 0, 1)">、字体样式宋体:SimSun

</span>7<span style="color: rgba(0, 0, 0, 1)">、字体样式新宋体:NSimSun

</span>8<span style="color: rgba(0, 0, 0, 1)">、字体样式仿宋:FangSong

</span>9<span style="color: rgba(0, 0, 0, 1)">、字体样式楷体:KaiTi

</span>10<span style="color: rgba(0, 0, 0, 1)">、字体样式仿宋_GB2312:FangSong_GB2312

</span>11<span style="color: rgba(0, 0, 0, 1)">、字体样式楷体_GB2312:KaiTi_GB2312

</span>12<span style="color: rgba(0, 0, 0, 1)">、字体样式微软正黑体:Microsoft JhengHei

</span>13<span style="color: rgba(0, 0, 0, 1)">、字体样式微软雅黑体:Microsoft YaHei

</span>14<span style="color: rgba(0, 0, 0, 1)">、字体样式隶书:LiSu

</span>15<span style="color: rgba(0, 0, 0, 1)">、字体样式幼圆:YouYuan

</span>16<span style="color: rgba(0, 0, 0, 1)">、字体样式华文细黑:STXihei

</span>17<span style="color: rgba(0, 0, 0, 1)">、字体样式华文楷体:STKaiti

</span>18<span style="color: rgba(0, 0, 0, 1)">、字体样式华文宋体:STSong

</span>19<span style="color: rgba(0, 0, 0, 1)">、字体样式华文中宋:STZhongsong

</span>20<span style="color: rgba(0, 0, 0, 1)">、字体样式华文仿宋:STFangsong

</span>21<span style="color: rgba(0, 0, 0, 1)">、字体样式方正舒体:FZShuTi

</span>22<span style="color: rgba(0, 0, 0, 1)">、字体样式方正姚体:FZYaoti

</span>23<span style="color: rgba(0, 0, 0, 1)">、字体样式华文彩云:STCaiyun

</span>24<span style="color: rgba(0, 0, 0, 1)">、字体样式华文琥珀:STHupo

</span>25<span style="color: rgba(0, 0, 0, 1)">、字体样式华文隶书:STLiti

</span>26<span style="color: rgba(0, 0, 0, 1)">、字体样式华文行楷:STXingkai

</span>27、字体样式华文新魏:STXinwei</pre>
</div>
<span class="cnblogs_code_collapse">字体样式</span></div>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> graphviz <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Digraph

dot </span>= Digraph(name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyPicture</span><span style="color: rgba(128, 0, 0, 1)">"</span>, format=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">png</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.node(name</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">A</span><span style="color: rgba(128, 0, 0, 1)">"</span>, label=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">老师</span><span style="color: rgba(128, 0, 0, 1)">"</span>, fontname=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Microsoft YaHei</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.node(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">学生</span><span style="color: rgba(128, 0, 0, 1)">'</span>, fontname=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Microsoft YaHei</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.edge(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">A</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">B</span><span style="color: rgba(128, 0, 0, 1)">"</span>, label=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">教学</span><span style="color: rgba(128, 0, 0, 1)">"</span>, fontname=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Microsoft YaHei</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.render(filename</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyPicture</span><span style="color: rgba(128, 0, 0, 1)">"</span>)</pre>
</div>
<p>&nbsp;</p>
<h4>3、无向图</h4>
<p>用法跟有向图一样</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> graphviz <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Graph

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 无向图</span>
dot = Graph(name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyPicture</span><span style="color: rgba(128, 0, 0, 1)">"</span>, format=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">png</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)

dot.node(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">People</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.node(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Home</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.edge(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">People</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Home</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.view(filename</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyPicture</span><span style="color: rgba(128, 0, 0, 1)">"</span>)</pre>
</div>
<p>&nbsp;</p>
<h3>三、属性(样式)说明</h3>
<h4>node节点属性如下&nbsp;</h4>
<table class="table table-bordered">
<thead>
<tr><th width="19%">Name</th><th width="21%">Default</th><th width="60%">Values</th></tr>
</thead>
<tbody>
<tr>
<td>color</td>
<td>black</td>
<td>node shape color</td>
</tr>
<tr>
<td>comment</td>
<td>&nbsp;</td>
<td>any string (format-dependent)</td>
</tr>
<tr>
<td>distortion</td>
<td>0.0</td>
<td>node distortion for shape=polygon</td>
</tr>
<tr>
<td>fillcolor</td>
<td>lightgrey/black</td>
<td>node fill color</td>
</tr>
<tr>
<td>fixedsize</td>
<td>false</td>
<td>label text has no affect on node size</td>
</tr>
<tr>
<td>fontcolor</td>
<td>black</td>
<td>type face color</td>
</tr>
<tr>
<td>fontname</td>
<td>Times-Roman</td>
<td>font family</td>
</tr>
<tr>
<td>fontsize</td>
<td>14</td>
<td>point size of label</td>
</tr>
<tr>
<td>group</td>
<td>&nbsp;</td>
<td>name of node’s group</td>
</tr>
<tr>
<td>height</td>
<td>.5</td>
<td>height in inches</td>
</tr>
<tr>
<td>label</td>
<td>node name</td>
<td>any string</td>
</tr>
<tr>
<td>layer</td>
<td>overlay range</td>
<td>all, id or id:id</td>
</tr>
<tr>
<td>orientation</td>
<td>0.0</td>
<td>node rotation angle</td>
</tr>
<tr>
<td>peripheries</td>
<td>shape-dependent</td>
<td>number of node boundaries</td>
</tr>
<tr>
<td>regular</td>
<td>false</td>
<td>force polygon to be regular</td>
</tr>
<tr>
<td>shape</td>
<td>ellipse</td>
<td>node shape; see Section 2.1 and Appendix E</td>
</tr>
<tr>
<td>shapefile</td>
<td>&nbsp;</td>
<td>external EPSF or SVG custom shape file</td>
</tr>
<tr>
<td>sides</td>
<td>4</td>
<td>number of sides for shape=polygon</td>
</tr>
<tr>
<td>skew</td>
<td>0.0</td>
<td>skewing of node for shape=polygon</td>
</tr>
<tr>
<td>style</td>
<td>&nbsp;</td>
<td>graphics options, e.g. bold, dotted, filled; cf. Section 2.3</td>
</tr>
<tr>
<td>URL</td>
<td>&nbsp;</td>
<td>URL associated with node (format-dependent)</td>
</tr>
<tr>
<td>width</td>
<td>.75</td>
<td>width in inches</td>
</tr>
<tr>
<td>z</td>
<td>0.0</td>
<td>z coordinate for VRML output</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>edge边框属性</h4>
<table class="table table-bordered">
<thead>
<tr><th width="19%">Name</th><th width="18%">Default</th><th width="63%">Values</th></tr>
</thead>
<tbody>
<tr>
<td>arrowhead</td>
<td>normal</td>
<td>style of arrowhead at head end</td>
</tr>
<tr>
<td>arrowsize</td>
<td>1.0</td>
<td>scaling factor for arrowheads</td>
</tr>
<tr>
<td>arrowtail</td>
<td>normal</td>
<td>style of arrowhead at tail end</td>
</tr>
<tr>
<td>color</td>
<td>black</td>
<td>edge stroke color</td>
</tr>
<tr>
<td>comment</td>
<td>&nbsp;</td>
<td>any string (format-dependent)</td>
</tr>
<tr>
<td>constraint</td>
<td>true</td>
<td>use edge to affect node ranking</td>
</tr>
<tr>
<td>decorate</td>
<td>&nbsp;</td>
<td>if set, draws a line connecting labels with their edges</td>
</tr>
<tr>
<td>dir</td>
<td>forward</td>
<td>forward, back, both, or none</td>
</tr>
<tr>
<td>fontcolor</td>
<td>black</td>
<td>type face color</td>
</tr>
<tr>
<td>fontname</td>
<td>Times-Roman</td>
<td>font family</td>
</tr>
<tr>
<td>fontsize</td>
<td>14</td>
<td>point size of label</td>
</tr>
<tr>
<td>headlabel</td>
<td>&nbsp;</td>
<td>label placed near head of edge</td>
</tr>
<tr>
<td>headport</td>
<td>&nbsp;</td>
<td>n,ne,e,se,s,sw,w,nw</td>
</tr>
<tr>
<td>headURL</td>
<td>&nbsp;</td>
<td>URL attached to head label if output format is ismap</td>
</tr>
<tr>
<td>label</td>
<td>&nbsp;</td>
<td>edge label</td>
</tr>
<tr>
<td>labelangle</td>
<td>-25.0</td>
<td>angle in degrees which head or tail label is rotated off edge</td>
</tr>
<tr>
<td>labeldistance</td>
<td>1.0</td>
<td>scaling factor for distance of head or tail label from node</td>
</tr>
<tr>
<td>labelfloat</td>
<td>false</td>
<td>lessen constraints on edge label placement</td>
</tr>
<tr>
<td>labelfontcolor</td>
<td>black</td>
<td>type face color for head and tail labels</td>
</tr>
<tr>
<td>labelfontname</td>
<td>Times-Roman</td>
<td>font family for head and tail labels</td>
</tr>
<tr>
<td>labelfontsize</td>
<td>14</td>
<td>point size for head and tail labels</td>
</tr>
<tr>
<td>layer</td>
<td>overlay range</td>
<td>all, id or id:id</td>
</tr>
<tr>
<td>lhead</td>
<td>&nbsp;</td>
<td>name of cluster to use as head of edge</td>
</tr>
<tr>
<td>ltail</td>
<td>&nbsp;</td>
<td>name of cluster to use as tail of edge</td>
</tr>
<tr>
<td>minlen</td>
<td>1</td>
<td>minimum rank distance between head and tail</td>
</tr>
<tr>
<td>samehead</td>
<td>&nbsp;</td>
<td>tag for head node; edge heads with the same tag are</td>
</tr>
<tr>
<td>sametail</td>
<td>&nbsp;</td>
<td>merged onto the same port</td>
</tr>
<tr>
<td>style</td>
<td>&nbsp;</td>
<td>tag for tail node; edge tails with the same tag are merged onto the same port</td>
</tr>
<tr>
<td>taillabel</td>
<td>&nbsp;</td>
<td>graphics options, e.g. bold, dotted, filled; cf. Section 2.3</td>
</tr>
<tr>
<td>tailport</td>
<td>&nbsp;</td>
<td>label placed near tail of edge n,ne,e,se,s,sw,w,nw</td>
</tr>
<tr>
<td>tailURL</td>
<td>&nbsp;</td>
<td>URL attached to tail label if output format is ismap</td>
</tr>
<tr>
<td>weight</td>
<td>1</td>
<td>integer cost of stretching an edge</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>Digraph图属性如下</h4>
<table class="table table-bordered">
<thead>
<tr><th width="18%">Name</th><th width="17%">Default</th><th width="65%">Values</th></tr>
</thead>
<tbody>
<tr>
<td>bgcolor</td>
<td>&nbsp;</td>
<td>background color for drawing, plus initial fill color</td>
</tr>
<tr>
<td>center</td>
<td>false</td>
<td>center drawing on page</td>
</tr>
<tr>
<td>clusterrank</td>
<td>local</td>
<td>may be global or none</td>
</tr>
<tr>
<td>color</td>
<td>black</td>
<td>for clusters, outline color, and fill color if fillcolor not defined</td>
</tr>
<tr>
<td>comment</td>
<td>&nbsp;</td>
<td>any string (format-dependent)</td>
</tr>
<tr>
<td>compound</td>
<td>false</td>
<td>allow edges between clusters</td>
</tr>
<tr>
<td>concentrate</td>
<td>false</td>
<td>enables edge concentrators</td>
</tr>
<tr>
<td>fillcolor</td>
<td>black</td>
<td>cluster fill color</td>
</tr>
<tr>
<td>fontcolor</td>
<td>black</td>
<td>type face color</td>
</tr>
<tr>
<td>fontname</td>
<td>Times-Roman</td>
<td>font family</td>
</tr>
<tr>
<td>fontpath</td>
<td>&nbsp;</td>
<td>list of directories to search for fonts</td>
</tr>
<tr>
<td>fontsize</td>
<td>14</td>
<td>point size of label</td>
</tr>
<tr>
<td>label</td>
<td>&nbsp;</td>
<td>any string</td>
</tr>
<tr>
<td>labeljust</td>
<td>centered</td>
<td>”l” and ”r” for left- and right-justified cluster labels, respectively</td>
</tr>
<tr>
<td>labelloc</td>
<td>top</td>
<td>”t” and ”b” for top- and bottom-justified cluster labels, respectively</td>
</tr>
<tr>
<td>layers</td>
<td>&nbsp;</td>
<td>id:id:id…</td>
</tr>
<tr>
<td>margin</td>
<td>.5</td>
<td>margin included in page, inches</td>
</tr>
<tr>
<td>mclimit</td>
<td>1.0</td>
<td>scale factor for mincross iterations</td>
</tr>
<tr>
<td>nodesep</td>
<td>.25</td>
<td>separation between nodes, in inches.</td>
</tr>
<tr>
<td>nslimit</td>
<td>&nbsp;</td>
<td>if set to f, bounds network simplex iterations by (f)(number of nodes) when setting x-coordinates</td>
</tr>
<tr>
<td>nslimit1</td>
<td>&nbsp;</td>
<td>if set to f, bounds network simplex iterations by (f)(number of nodes) when ranking nodes</td>
</tr>
<tr>
<td>ordering</td>
<td>&nbsp;</td>
<td>if out out edge order is preserved</td>
</tr>
<tr>
<td>orientation</td>
<td>portrait</td>
<td>if rotate is not used and the value is landscape, use landscape orientation</td>
</tr>
<tr>
<td>page</td>
<td>&nbsp;</td>
<td>unit of pagination, e.g. “8.5,11”</td>
</tr>
<tr>
<td>pagedir</td>
<td>BL</td>
<td>traversal order of pages</td>
</tr>
<tr>
<td>quantum</td>
<td>&nbsp;</td>
<td>if quantum ¿ 0.0, node label dimensions will be rounded to integral multiples of quantum</td>
</tr>
<tr>
<td>rank</td>
<td>&nbsp;</td>
<td>same, min, max, source or sink</td>
</tr>
<tr>
<td>rankdir</td>
<td>TB</td>
<td>LR (left to right) or TB (top to bottom)</td>
</tr>
<tr>
<td>ranksep</td>
<td>.75</td>
<td>separation between ranks, in inches.</td>
</tr>
<tr>
<td>ratio</td>
<td>&nbsp;</td>
<td>approximate aspect ratio desired, fill or auto</td>
</tr>
<tr>
<td>remincross</td>
<td>&nbsp;</td>
<td>if true and there are multiple clusters, re-run crossing minimization</td>
</tr>
<tr>
<td>rotate</td>
<td>&nbsp;</td>
<td>If 90, set orientation to landscape</td>
</tr>
<tr>
<td>samplepoints</td>
<td>8</td>
<td>number of points used to represent ellipses and circles on output (cf. Appendix C</td>
</tr>
<tr>
<td>searchsize</td>
<td>30</td>
<td>maximum edges with negative cut values to check when looking for a minimum one during network simplex</td>
</tr>
<tr>
<td>size</td>
<td>&nbsp;</td>
<td>maximum drawing size, in inches</td>
</tr>
<tr>
<td>style</td>
<td>&nbsp;</td>
<td>graphics options, e.g. filled for clusters</td>
</tr>
<tr>
<td>URL</td>
<td>&nbsp;</td>
<td>URL associated with graph (format-dependent)</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>例子</h4>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 0, 1)">"""</span><span style="color: rgba(128, 0, 0, 1)">使用graph_attr, node_attr, edge_attr参数, 你可以更改图中节点和边的显示样式</span><span style="color: rgba(128, 0, 0, 1)">"""</span>

<span style="color: rgba(0, 0, 255, 1)">from</span> graphviz <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Digraph

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 可以在实例化对象的时候设置样式</span>
dot = Digraph(name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyPicture</span><span style="color: rgba(128, 0, 0, 1)">"</span>, node_attr={<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">plaintext</span><span style="color: rgba(128, 0, 0, 1)">"</span>}, format=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">png</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 也可以实例化之后, 设置这些样式</span>
dot.graph_attr[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">rankdir</span><span style="color: rgba(128, 0, 0, 1)">'</span>] = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">LR</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
dot.edge_attr.update(arrowhead</span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">vee</span><span style="color: rgba(128, 0, 0, 1)">'</span>, arrowsize=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 然后开始画图</span>
dot.node(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Dog</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.node(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Cat</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
dot.edge(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Dog</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Cat</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)

dot.view(filename</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyPicture</span><span style="color: rgba(128, 0, 0, 1)">"</span>)</pre>
</div>
<p>&nbsp;</p>
<h3>四、官网</h3>
<p>官方文档</p>
<p>官网</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/Zzbj/p/11431015.html
頁: [1]
查看完整版本: python graphviz的使用(画图工具)