图形可视化工具
项目描述
树维泽
使用Graphviz可视化节点数据结构。
这个怎么运作
Treevizer 迭代您的数据结构以生成 DOT 文件并运行 Graphviz 将其转换为图像。数据结构需要使用节点构建。
它还支持递归函数。
例子
链表
平衡二叉树
特里
递归斐波那契
先决条件
您需要安装Graphviz并确保它位于 $PATH 中。
赛格温
Graphviz 和 Pillow 需要在 Cygwin 上进行特殊安装。
图形可视化
不要在 Cygwin 中安装 Graphviz,进行 windows 安装。否则路径会有问题。
枕头
Pillow 是可选的,它仅用于创建 GIF。如果您不想创建 GIF,则无需安装它。
不要使用 pip 安装 Pillow,而是使用 Cygwin 包来安装。安装 Cygwin 包python3X-imaging,替换X为你的 Python 版本。
安装
pip install treevizer
用法
支持以下结构:
链表 (ll)
结构类型称为“ll”。配置键称为“LinkedList”。
需要具有满足以下类图的属性的 Node 类。
平衡二叉树 (bbt)
结构类型称为“bbt”。配置键称为“BalancedBinaryTree”。
需要具有满足以下类图的属性的 Node 类。
特里(特里)
结构类型称为“trie”。配置键称为“Trie”。
需要具有满足以下类图的属性的 Node 类。
该stop属性标记节点是否标记单词(True)或不标记(False)。
递归
用 . 装饰你的递归函数recursion_viz。可以装饰多种功能。在创建 PNG 或 GIF 时,每个函数都由函数名称标识。
例子
import treevizer
@treevizer.recursion_viz
def a_rec_func():
a_rec_func()
@treevizer.recursion_viz
def another_rec_func():
another_rec_func()
a_rec_func()
another_rec_func()
treevizer.recursion_to_png("a_rec_func", dot_path="recursion.dot", png_path="recursion.png")
treevizer.recursion_to_png("another_rec_func", dot_path="rec2.dot", png_path="rec2.png")
要配置点语言,请使用“递归”作为 json 中的键。
功能
DOT 文件的结构
import treevizer
treevizer.to_dot(root, structure_type="bbt", dot_path="tree.dot")
"""
Generate DOT file from node structure.
Parameters
----------
root : Node
Root node for datastructure
structure_type : str
Name of the type of datastructure (default is "bbt")
dot_path : str
Path to generated DOT file (default is tree.dot)
"""
结构转为 PNG
这也会创建一个 DOT 文件。
import treevizer
treevizer.to_png(root, structure_type="bbt", dot_path="tree.dot", png_path="tree.png")
"""
Generate DOT file from node structure and use Graphviz to create image.
Parameters
----------
root : Node
Root node for datastructure
structure_type : str
Name of the type of datastructure (default is "bbt")
dot_path : str
Path to generated DOT file (default is tree.dot)
png_path : str
Path to generated png file (default is tree.png)
"""
递归到 PNG
这也会创建一个 DOT 文件。
import treevizer
@treevizer.recursion_viz
def a_recusive_function():
a_recusive_function()
treevizer.recursion_to_png(function_name, dot_path="recursion.dot", png_path="recursion.png")
"""
Generate DOT file of recursive function calls and use Graphviz to create image.
Parameters
----------
function_name : str
Name of your decorated function.
dot_path : str
Path to generated DOT file (default is recursion.dot)
png_path : str
Path to generated png file (default is recursion.png)
"""
递归到 GIF
import treevizer
@treevizer.recursion_viz
def a_recusive_function():
a_recusive_function()
treevizer.recursion_to_gif(function_name, gif_path="recursion.dot", duration=800, loop=0)
"""
Generate PNG files of recursive function calls and use Pillow to create GIF.
Parameters
----------
function_name : str
Name of your decorated function.
gif_path : str
Path to generated GIF file (default is recursion.gif)
duration : int
duration, in miliseconds, of how long each image will be displayed in the GIF (default is 800)
loop : int
how many times the GIF should loop after initial loop. 0 to loop infinitely (default is 0)
"""
点文件转PNG
import treevizer
treevizer.dot_to_png(dot_path="tree.dot", png_path="tree.png")
"""
Use Graphviz to create image from a DOT file.
Parameters
----------
dot_path : str
Path to your DOT file (default is tree.dot)
png_path : str
Path to generated png file (default is tree.png)
"""
配置
在根文件夹中创建.dot.json以更改 DOT 配置。可以在Graphviz 文档中找到可用的选项。
例如,要更改平衡二叉树的图像中节点的颜色,请使用以下命令。
# .dot.json
{
"BalancedBinaryTree": {
"node": {
"fillcolor": "green"
}
}
}
要更改链接列表中节点的大小和形状以及边缘的颜色,请使用以下命令。
# .dot.json
{
"LinkedList": {
"node": {
"shape": "square",
"width": 1.5
},
"edge": {
"color": "red"
}
}
}
更改边缘标签上的字体颜色以进行递归。
{
"Recursion": {
"edge": {
"fontcolor": "black"
}
}
}
已知错误/警告
链接
执照
该项目在 MIT 许可下获得许可 - 请参阅LICENSE.md文件了解详细信息