---
# System prepended metadata

title: Python graphviz を利用する 2021-07-22
tags: [Python, graphviz]

---

---
lang: ja-jp
breaks: true
---
# Python graphviz を利用する 2021-07-22

## 参考

> Anaconda環境でGraphvizを使う
> https://qiita.com/nemutas/items/4f7bd96f63cf78bdb357

## Graphviz のインストール

https://graphviz.org/download/
	https://www2.graphviz.org/Packages/stable/windows/10/msbuild/Release/Win32/graphviz-2.44.1-win32.zip

以下のインストーラーを利用するとPATHの設定の手間が省ける。

https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/2.48.0/stable_windows_10_cmake_Release_x64_graphviz-install-2.48.0-win64.exe

![](https://i.imgur.com/skfYULj.png)

:::info
手動で環境変数を登録する場合は、`C:\Program Files\Graphviz\bin`を`PATH`に追加する。
![](https://i.imgur.com/IR73aIh.png)
:::

## 動作確認

```python=
from graphviz import Digraph
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

G = Digraph(format="png")
G.attr("node", shape="square", style="filled")
G.edge("start","state1",label="0.8")
G.edge("start","state2",label="0.2")
G.edge("state1","state1",label="0.5")
G.edge("state2","state2", label="0.8")
G.edge("state1","state2",label="0.5")
G.edge("state2","end",label="0.2")
G.edge("end","count",label="1.0")
G.edge("count","start",label="1.0")
G.node("start", shape="circle", color="pink")
G.render("graphs")

im = Image.open("./graphs.png")
im_list = np.asarray(im)
plt.imshow(im_list)
plt.show()
```

![](https://i.imgur.com/ejRwGHF.png)


###### tags: `Python` `graphviz`
