Graphviz

學習一時爽,一直學習一直爽linux

  Hello,你們好,我是 もうり,一個從無到有的技術+語言小白。web

官方文檔:http://www.graphviz.org算法

graphviz是貝爾實驗室開發的一個開源的工具包,它使用一個特定的DSL(領域特定語言):dot做爲腳本語言,而後使用佈局引擎來解析此腳本,並完成自動佈局。spring

Graphviz

graphviz自己是一個繪圖工具軟件,下載地址在:http://www.graphviz.org/。若是你是linux,能夠用apt-get或者yum的方法安裝。若是是windows,就在官網下載msi文件安裝。不管是linux仍是windows,裝完後都要設置環境變量,將graphviz的bin目錄加到PATH,好比我是windows,加入了PATHwindows

在這裏插入圖片描述

如何佈局

graphviz中包含了衆多的佈局器:微信

  • dot 默認佈局方式,主要用於有向圖app

  • neato 基於spring-model(又稱force-based)算法工具

  • twopi 徑向佈局佈局

  • circo 圓環佈局學習

  • graph用於無向圖

會個dot和graph就能裝逼了

要用graphviz畫圖,首先要明確的就是所畫之圖爲有向圖仍是無向圖。

Digraph表示有向圖,graph表示無向圖。

通常來講,主要是有向圖,無向圖也可經過設置邊的屬性來畫出無向邊。

須注意的是,-> 表示有向圖中的邊,-- 表示無向圖中的邊,不能混用。

有向圖圖

好比,要繪製一個有向圖,包含4個節點a,b,c,d。

其中a指向b,b和c指向d。能夠定義下列腳本:

創建一個demo.dot腳本:

digraph demo{
a->b;
b->d;
c->d;
}
在這裏插入圖片描述

而後保存下

在這裏插入圖片描述

那麼這個demo.png就生成了

在這裏插入圖片描述

打開Graphviz好麻煩,沒錯還能夠直接用cmd

打開cmd到first.dot目錄下,運行:
dot -Tpng demo.dot -o demo.png
能夠獲得畫好的圖形。

解釋:dot表示使用的是dot佈局,其餘佈局相應的修改便可,-T表示格式,即畫成png格式,-o表示重命名爲first.png。

無向圖

就是將->變成 --

graph demo1 {
    a -- b
    b -- c
    c -- a
}
在這裏插入圖片描述

恭喜你入門了,我也就會個dot

http://icodeit.org/2012/01/%E4%BD%BF%E7%94%A8graphviz%E7%BB%98%E5%88%B6%E6%B5%81%E7%A8%8B%E5%9B%BE/

後面就是學下換樣式換圖形

在這裏插入圖片描述

我得說下hash表就是經過這玩意畫的

在這裏插入圖片描述
在這裏插入圖片描述

py交互

主要是將一個決策樹可視化

sklearn自帶的 export_graphviz

使用的包是pydotplus

pip install pydotplus

demo

# author: 毛利
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from  sklearn import tree
from sklearn.metrics import accuracy_score
import pydotplus
iris = datasets.load_iris()
iris_feature = '花萼長度''花萼寬度''花瓣長度''花瓣寬度'
iris_feature_E = 'sepal length''sepal width''petal length''petal width'
iris_class = 'Iris-setosa''Iris-versicolor''Iris-virginica'
x = pd.DataFrame(iris['data'])[[0,1]]
y = iris.target
x_train,x_test,y_train,y_test = train_test_split(x,y)
model = DecisionTreeClassifier()
model.fit(x_train,y_train)
y_train_pred = model.predict(x_train)
print('訓練集正確率:', accuracy_score(y_train, y_train_pred))

# 保存
# dot -Tpng my.dot -o my.png
1、輸出
with open('iris.dot''w') as f:
    tree.export_graphviz(model, out_file=f, feature_names=iris_feature_E[0:2], class_names=iris_class,
                         filled=True, rounded=True, special_characters=True)
tree.export_graphviz(model, out_file='iris.dot', feature_names=iris_feature_E[0:2], class_names=iris_class,
                     filled=True, rounded=True, special_characters=True)
2、給定文件名
tree.export_graphviz(model, out_file='iris.dot')
# tree.export_graphviz(model, out_file='iris.dot')
3、輸出爲pdf格式
dot_data = tree.export_graphviz(model, out_file=None, feature_names=iris_feature_E[0:2], class_names=iris_class,
                                filled=True, rounded=True, special_characters=True)

graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf('iris.pdf')
f = open('iris.png''wb')
f.write(graph.create_png())
f.close()







本文分享自微信公衆號 - Python之王(sen13717378202)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索