GraphViz :1 安装和简单使用

概要

  • GraphViz是啥?GraphViz是一个开源的图像可视化的软件,是贝尔实验室开发的一个开源的工具包,它使用一个特定的DSL(领域特定语言): dot作为脚本语言,然后使用布局引擎来解析此脚本,并完成自动布局。
  • 不同于matplotlib,前者是坐标形式的图像,后者是结构型图像。
  • graphviz提供丰富的导出格式,如常用的图片格式,SVG,PDF格式等。
  • 总之,GraphViz可在Win10的cmd调用,也可以在Python通过脚本调用,比较灵活。

1 安装GraphViz

分做三个步骤:

1 安装dll

conda install GraphViz

2 安装与python相关的东东

pip install graphviz

3 在anaconda安装路径中找到C:\Anaconda3\Library\bin\graphviz,将此路径添加至系统环境变量中(环境变量:计算机-属性-高级系统设置-环境变量)

 2 在python中使用GraphViz

将下面代码粘贴到python程序。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# .Data:.2019/12/27
# -*- coding: utf-8 -*-

from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import pydotplus

import warnings
warnings.filterwarnings("ignore")

#加载数据
iris = datasets.load_iris()

#构建模型
fls = DecisionTreeClassifier()
fls = fls.fit(iris.data,iris.target)

#保存模型
with open('iris.dot','w') as f:
    f = tree.export_graphviz(fls,out_file=f)

#画图,保存到pdf文件中

#设置图像参数
dot_data = tree.export_graphviz(fls,out_file=None,
                                feature_names=iris.feature_names,
                                class_names=iris.target_names,
                                filled=True,rounded=True,special_characters=True)

graph = pydotplus.graph_from_dot_data(dot_data)

#保存图像到pdf文件
graph.write_pdf('iris.pdf')

即将产生iris.pdf文件,打开后,有所产生的图形。

3 在Win10的命令行使用GraphViz

其实给定任意* .dot文件,就可以用 dot     -Tjpg    ./xxxx.dot    -o  output.jpg指令生成图片。

dot     -T   jpg    ./grph.dot  -o   graph01.jpg                          # 生成一个图片。

参看资料:

GraphViz的其它用途:GraphViz的使用 - 简书 (jianshu.com)

使用Graphviz和DOT语言绘图-百度经验 (baidu.com)

おすすめ

転載: blog.csdn.net/gongdiwudu/article/details/119317706
おすすめ