2021-12-12に更新

PythonでGraphvizってどうやって使うんだぜ(^~^)?

わちゃー(^~^) 公開下書き

ramen-tabero-futsu2.png
「 Python で Graphviz ってどうやって使うんだぜ?」

kifuwarabe-futsu.png
「 もう寝ろ」

📖 Python上でGraphvizを使って綺麗なグラフを描く
📖 pythonでgraphvizの使い方(美しいツリー構造や状態遷移図の描画)

ohkina-hiyoko-futsu2.png
「 👆 この記事を読んだらいいんじゃないの?」

ramen-tabero-futsu2.png
「 Windows に Graphviz をインストールするには どうやったらいいんだぜ?」

📖 Download

kifuwarabe-futsu.png
「 👆 ここからダウンロードしろだぜ」

ramen-tabero-futsu2.png
「 インストールしたぜ」

pip install graphviz

kifuwarabe-futsu.png
「 👆 じゃあ Python でも使えるようにインストールしろだぜ」

ramen-tabero-futsu2.png
「 インストールしたぜ」

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath('dot'), make sure the Graphviz executables are on your systems' PATH

ramen-tabero-futsu2.png
「 👆 動かないぜ」

ohkina-hiyoko-futsu2.png
「 PATH を設定しなくちゃいけないんじゃないの?」

ramen-tabero-futsu2.png
「 インストール時に設定したぜ」

kifuwarabe-futsu.png
「 Visual Studio Code を全て閉じてから ターミナルを開けだぜ」

20211212blog6.png

ramen-tabero-futsu2.png
「 👆 おおー、出た」

ohkina-hiyoko-futsu2.png
「 じゃあ試しに CSAサーバープロトコル1.2.1 を描いてみてちょうだい」

20211212blog7.png

ramen-tabero-futsu2.png
「 👆 見にくいけど、図は描いてくれるぜ」

kifuwarabe-futsu.png
「 サブステートは描けるのかだぜ? 階層化が必要だぜ」

20211212blog8a1.png

from graphviz import Digraph

# グラフの設定
g = Digraph(format="png")
g.attr("node", shape="square", style="filled")

# クラスター 'cluster_' から名前を始める必要あり
with g.subgraph(name='cluster_root') as c:
    # 一番外側のクラスターのラベルは図のタイトルのように見える
    c.attr(color='white', label='CSA Server protocol 1.2.1')

    # 開始ノード
    c.node("Start", shape="circle", color="pink")

    # 2つのノードと、その二間の有向な辺
    c.edge("Start", "1", label="Start")

    with c.subgraph(name='cluster_init') as c2:
        c2.attr(color='pink', label='Init')
        c2.node('1')
        c2.node('Login')
        c2.edge('1', 'Login', label='login')
        c2.edge('Login', '1', label='incorrect')

    with c.subgraph(name='cluster_lobby') as c2:
        c2.attr(color='pink', label='Lobby')
        c2.node('2')
        c2.node('Logout')
        c2.edge("2", "Logout", label="logout")

    with c.subgraph(name='cluster_reply') as c2:
        c2.attr(color='pink', label='Reply')
        c2.node('3')
        c2.node('Agree')
        c2.node('Reject')
        c2.edge("3", "Agree", label="agree")
        c2.edge("3", "Reject", label="reject")

    with c.subgraph(name='cluster_game') as c2:
        c2.attr(color='pink', label='Game')
        c2.node('4')
        c2.edge("4", "4", label="Move")
        c2.edge("4", "4", label="MoveEcho")

    c.edge("Login", "2", label="ok")
    c.edge("Logout", "1", label="completed")
    c.edge("Reject", "2", label="reject")
    c.edge("2", "3", label="game_summary")
    c.edge("Agree", "4", label="start")
    c.edge("4", "2", label="GameoverWcsc")
    c.edge("4", "1", label="GameoverFloodgate")

# 描画
g.render("graphs")

ramen-tabero-futsu2.png
「 👆 描けるけど、図は見にくいぜ」

kifuwarabe-futsu.png
「 よしとしろだぜ」

20211212blog8a2.png

ramen-tabero-futsu2.png
「 👆 色指定 文字に できないな」

ohkina-hiyoko-futsu2.png
「 エンジニアには デザイナーのこだわりは 脳が知覚できないのよ」

何度でもクリック!→

むずでょ

光速のアカウント凍結されちゃったんで……。ゲームプログラムを独習中なんだぜ☆電王戦IIに出た棋士もコンピューターもみんな好きだぜ☆▲(パソコン将棋)WCSC29一次予選36位、SDT5予選42位▲(パソコン囲碁)AI竜星戦予選16位

Crieitは個人で開発中です。 興味がある方は是非記事の投稿をお願いします! どんな軽い内容でも嬉しいです。
なぜCrieitを作ろうと思ったか

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください!

ボードとは?

むずでょ の最近の記事