Python中模块graphviz使用入门

 更新时间:2025年05月02日 10:37:38   作者:编程零零七  
graphviz是一个用于创建和操作图形的 Python 库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一下

graphviz 是一个用于创建和操作图形(如流程图、网络图等)的 Python 库。它依赖于 Graphviz 软件包,后者是一个开源的图形可视化软件。graphviz 库允许你从 Python 脚本中生成 Graphviz 的点文件(DOT 文件),并渲染成图像。

以下是使用 graphviz 的基本步骤:

1.安装

1.1 安装 Graphviz 软件首先,你需要在你的系统上安装 Graphviz 软件包。你可以从 Graphviz 官网 下载并安装它。安装完成后,确保 dot 命令在你的系统路径中可用。

1.2 安装 Python 的 graphviz 库你可以使用 pip 来安装 Python 的 graphviz 库:

pip install graphviz

2. 基本用法

该graphviz模块提供了两个类:Graph和 Digraph。它们分别以DOT语言为无向图和有向图创建图描述。它们具有相同的 API。通过实例化一个new Graph或 Digraphobject 创建一个图形:

from graphviz import Digraph

dot = Digraph(comment='The Round Table')

print(dot)

输出如下信息

// The Round Table
digraph {
}

然后我们可以添加点和边,通过node()和edge()或edges()来实现。

from graphviz import Digraph

dot = Digraph(comment='The Round Table')
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')

dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false')
print(dot.source)  

生成的源代码如下:

// The Round Table
digraph {
    A [label="King Arthur"]
    B [label="Sir Bedevere the Wise"]
    L [label="Sir Lancelot the Brave"]
    A -> B
    A -> L
    B -> L [constraint=false]
}

最后我们可以通过如下代码保存图像pdf文件,并显示。通过设置view=True将自动使用系统默认的文件类型的查看器应用程序打开生成的文件(PDF,PNG,SVG等)。

dot.render('test-output/round-table.gv', view=True)  

在这里插入图片描述

2.1 输出图像格式

要使用与默认PDF 不同的输出文件格式,请format在创建Graph或 Digraph对象时使用参数:

from graphviz import Graph

g = Graph(format='png')

或者在基本用法的例子中在输出中添加format='jpg’便可以获得jpg图像。

dot.render('test-output/round-table.gv',format='jpg', view=True)  

如果是想设置输出图像的dpi,需要在创建Graph或Digraph对象时,设置dpi参数。

from graphviz import Graph

g = Graph(format='png')
g.graph_attr['dpi'] = '300'

2.2 图像style设置

使用graph_attr,node_attr和 edge_attr参数更改默认外观的图表,点和连接线。

from graphviz import Digraph

ps = Digraph(name='pet-shop', node_attr={'shape': 'plaintext'},format='png')
ps.node('parrot')
ps.node('dead')
ps.edge('parrot', 'dead')

在这里插入图片描述

2.3 属性

要设置图中的所有后续图形,点或边的树形,请使用attr()方法,如下所示:

from graphviz import Digraph
from graphviz import Graph
ni = Graph('ni',format='jpg')

ni.attr('node', shape='rarrow')
ni.node('1', 'Ni!')
ni.node('2', 'Ni!')

ni.node('3', 'Ni!', shape='egg')

ni.attr('node', shape='star')
ni.node('4', 'Ni!')
ni.node('5', 'Ni!')
ni.attr(rankdir='LR')

ni.edges(['12', '23', '34', '45'])
print(ni.source) 
ni.view()

在这里插入图片描述

2.4 子图和聚类

图和有向图对象有一个subgraph()-用于向实例添加子图的方法。
有两种方法可以使用它:使用与唯一参数(其内容作为子图添加)类型相同的现成图形对象,或者省略图形参数(返回上下文管理器,以便在with块中更优雅地定义子图内容)。
第一个用法选项,只有graph作为参数:

from graphviz import Digraph
from graphviz import Graph
p = Graph(name='parent', node_attr={'shape': 'plaintext'},format='png')
p.edge('spam', 'eggs')

c = Graph(name='child', node_attr={'shape': 'box'})
c.edge('foo', 'bar')

p.subgraph(c)
p.view()

第二次使用,带有with-block(忽略graph参数):

p = Graph(name='parent')
p.edge('spam', 'eggs')

with p.subgraph(name='child', node_attr={'shape': 'box'}) as c:
    c.edge('foo', 'bar')

两者结果相同如下图所示:

在这里插入图片描述

3 实例

代表的实例图像如下所示

  • 有向图
    代码
from graphviz import Digraph
g = Digraph('G', filename='hello.gv',format='png')
g.edge('Hello', 'World')
g.view()

结果如图所示:

在这里插入图片描述

  • 无向图
    代码
from graphviz import Graph

g = Graph('G', filename='process.gv', engine='sfdp',format='png')

g.edge('run', 'intr')
g.edge('intr', 'runbl')
g.edge('runbl', 'run')
g.edge('run', 'kernel')
g.edge('kernel', 'zombie')
g.edge('kernel', 'sleep')
g.edge('kernel', 'runmem')
g.edge('sleep', 'swap')
g.edge('swap', 'runswap')
g.edge('runswap', 'new')
g.edge('runswap', 'runmem')
g.edge('new', 'runmem')
g.edge('sleep', 'runmem')

g.view()

结果如图所示:

在这里插入图片描述

  • 子图
    代码
from graphviz import Digraph

g = Digraph('G', filename='cluster.gv',format='png')

# NOTE: the subgraph name needs to begin with 'cluster' (all lowercase)
#       so that Graphviz recognizes it as a special cluster subgraph

with g.subgraph(name='cluster_0') as c:
    c.attr(style='filled', color='lightgrey')
    c.node_attr.update(style='filled', color='white')
    c.edges([('a0', 'a1'), ('a1', 'a2'), ('a2', 'a3')])
    c.attr(label='process #1')

with g.subgraph(name='cluster_1') as c:
    c.attr(color='blue')
    c.node_attr['style'] = 'filled'
    c.edges([('b0', 'b1'), ('b1', 'b2'), ('b2', 'b3')])
    c.attr(label='process #2')

g.edge('start', 'a0')
g.edge('start', 'b0')
g.edge('a1', 'b3')
g.edge('b2', 'a3')
g.edge('a3', 'a0')
g.edge('a3', 'end')
g.edge('b3', 'end')

g.node('start', shape='Mdiamond')
g.node('end', shape='Msquare')

g.view()

结果如图所示:

在这里插入图片描述

4 如何进一步使用python graphviz

python graphviz官方文档如下:

https://graphviz.readthedocs.io/en/stable/index.html

在实际使用时,参考官方实例就行。

实际上graphviz画一些流程图即可,而且需要较大的调整参数。因此如果非紧急绘图建议使用visio。

到此这篇关于Python中模块graphviz使用入门的文章就介绍到这了,更多相关Python 模块graphviz使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • PyTorch 检查GPU版本是否安装成功的操作

    PyTorch 检查GPU版本是否安装成功的操作

    这篇文章主要介绍了PyTorch 检查GPU版本是否安装成功的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • python知识:装饰器@property到底有啥用途

    python知识:装饰器@property到底有啥用途

    这篇文章主要介绍了python装饰器@property到底有啥用途,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Pandas DataFrame列快速转换为列表(3秒学会!)

    Pandas DataFrame列快速转换为列表(3秒学会!)

    这篇文章主要给大家介绍了关于Pandas DataFrame列如何快速转换为列表的相关资料,在Python的pandas库中可以使用DataFrame的tolist()方法将DataFrame转化为列表,需要的朋友可以参考下
    2023-10-10
  • python求质数的3种方法

    python求质数的3种方法

    这篇文章主要为大家详细介绍了python求质数的多种方法,多种方法求质数的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • Django rstful登陆认证并检查session是否过期代码实例

    Django rstful登陆认证并检查session是否过期代码实例

    这篇文章主要介绍了Django rstful登陆认证并检查session是否过期代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • 详解python环境安装selenium和手动下载安装selenium的方法

    详解python环境安装selenium和手动下载安装selenium的方法

    这篇文章主要介绍了详解python环境安装selenium和手动下载安装selenium的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • python实现根据月份和日期得到星座的方法

    python实现根据月份和日期得到星座的方法

    这篇文章主要介绍了python实现根据月份和日期得到星座的方法,涉及Python操作字符串及数组的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • Python实现外星人去哪了小游戏详细代码

    Python实现外星人去哪了小游戏详细代码

    今天为大家带来一款小游戏,名叫外星人去哪了,用Python语言实现完成,代码简洁易懂,感兴趣的小伙伴快来看看吧
    2022-03-03
  • 使用Python写一个量化股票提醒系统

    使用Python写一个量化股票提醒系统

    这篇文章主要介绍了小白用Python写了一个股票提醒系统,迷你版量化系统,完美的实现了实时提醒功能,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-08-08
  • Python应用之利用pyecharts画中国地图

    Python应用之利用pyecharts画中国地图

    这篇文章主要介绍了Python应用之利用pyecharts画中国地图,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07

最新评论