pip install graphviz
Requirement already satisfied: graphviz in /opt/conda/lib/python3.12/site-packages (0.20.3) Note: you may need to restart the kernel to use updated packages.
接下来是创建线性图的代码:
from graphviz import Digraph
创建一个有向图
dot = Digraph(comment='The Test Table')
添加节点
dot.node('A', 'A')
dot.node('B', 'B')
dot.node('C', 'C')
添加边
dot.edges(['AB', 'BC'])
打印生成的DOT源码
print(dot.source)
// The Test Table digraph { A [label=A] B [label=B] C [label=C] A -> B B -> C }
保存图像为 test-output/test-table.gv.pdf
文件
dot.render('test-output/test-table.gv', format='pdf', cleanup=True, view = True)
dot
from graphviz import Digraph
dot = Digraph(comment='The While Loop')
添加循环开始节点
dot.node('A', 'Start Loop')
dot.node('B', 'Do Stuff')
dot.node('C', 'Check Condition')
dot.node('D', 'End Loop')
添加边表示循环结构
dot.edges(['AB', 'BC', 'CD', 'DA'])
添加条件判断
dot.edge('C', 'B', constraint='true', label='true')
dot.edge('C', 'D', constraint='false', label='false')
打印生成的DOT源码
print(dot.source)
// The While Loop digraph { A [label="Start Loop"] B [label="Do Stuff"] C [label="Check Condition"] D [label="End Loop"] A -> B B -> C C -> D D -> A C -> B [label=true constraint=true] C -> D [label=false constraint=false] }
保存图像为 test-output/while-loop.gv.pdf
文件
dot.render('test-output/while-loop.gv', format='pdf', cleanup=True)
dot
from graphviz import Digraph
创建一个图形
dot = Digraph(comment='The Dynamic Graph')
初始添加节点
dot.node('A', 'Node A')
dot.node('B', 'Node B')
动态添加边和节点
dot.edge('A', 'B')
根据条件动态删除节点和边
dot.node('C', 'Node C')
假设这是某些条件判断的结果
if False:
dot.edge('B', 'C')
dot.edge('B', 'A', style='dotted')
打印生成的DOT源码
print(dot.source)
// The Dynamic Graph digraph { A [label="Node A"] B [label="Node B"] A -> B C [label="Node C"] B -> A [style=dotted] }
保存图像为 test-output/dynamic-graph.gv.pdf
文件
dot.render('test-output/dynamic-graph.gv', format='pdf', cleanup=True)
dot
from graphviz import Digraph
import os
创建一个基础图形模板
base_graph = Digraph(comment='Base Graph')
base_graph.node('A', 'Start')
base_graph.edge('A', 'B', label='Next')
批量处理的函数
def create_and_render_graph(index):
graph = base_graph.copy()
graph.node('B', f'Node {index}')
# graph.render(f'test-output/graph{index}', format='png', cleanup=True)
return graph.source
处理5个图形
for i in range(1, 6):
print(f'Creating and rendering graph{i}...')
create_and_render_graph(i)
Creating and rendering graph1... Creating and rendering graph2... Creating and rendering graph3... Creating and rendering graph4... Creating and rendering graph5...
from graphviz import Digraph
dot = Digraph(comment='The Example Graph')
添加节点和边
dot.node('A', 'Start')
dot.node('B', 'End')
dot.edge('A', 'B')
设置渲染格式并保存
# dot.render('test-output/example-graph', format='png', cleanup=True) # PNG 格式
# dot.render('test-output/example-graph', format='svg', cleanup=True) # SVG 格式
# dot.render('test-output/example-graph', format='pdf', cleanup=True) # PDF 格式
dot
from graphviz import Digraph
dot = Digraph(comment='The Colored Graph')
添加节点和边
dot.node('A', 'Start')
dot.node('B', 'End')
dot.edge('A', 'B', color='red')
使用Neato布局算法渲染图形
dot.attr(rankdir='LR', splines='ortho', color='blue', fontcolor='red')
dot.edges(['AB'])
dot.attr(size='6,3')
渲染图形为PNG格式
# dot.render('test-output/colored-graph', format='png', cleanup=True)
dot
在上述代码中,我们对图形进行了一些高级配置:
rankdir='LR'
设置了节点的排列方向,从左到右。splines='ortho'
指定了边的形状为直角连接。color='blue'
和fontcolor='red'
分别设置了图形和字体的颜色。size='6,3'
设置了渲染图形的大小。
通过这些高级选项,我们可以对渲染出的图形进行更细致的控制, 使得图形更贴合我们的展示需求。
到目前为止,我们已经详细介绍了如何使用 graphviz 库来创建、编辑和渲染图形, 并展示了具体的示例代码。在接下来的第六章中, 我们将深入探讨Graphviz的自动布局功能以及它支持的多种格式输出。