Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

PythonのGraphviz ノードの設定

公開日 2024-10-27

この記事ではPythonのGraphvizでノードを設定する方法を解説します。 ノードの形状、色、サイズやフォントを設定できます。

ノード設定の基本

ノードの設定方法には、グラフ全体でまとめて変更する方法と、個別のノード設定を変更する方法の2つがあります。

グラフ全体でまとめて変更する場合、グラフオブジェクトのattr()メソッドを使用します。attr()メソッドの第1引数を"node"として、第2引数以降に設定を記述します。以下に例を示します。

from graphviz import Graph

graph = Graph()
graph.attr("node", shape="box", color="blue")
graph.node("node1")
graph.node("node2")
graph.edge("node1", "node2")
graph
Loading...

一方、個別のノードごとに設定する場合、以下のようにグラフオブジェクトのnode()メソッドに設定を記述します。

graph = Graph()
graph.node("node1", shape="box", color="blue")
graph.node("node2")
graph.edge("node1", "node2")
graph
Loading...

以降では、ノードに設定可能な項目を解説します。

ノードの形状

ノードの形状はnode()メソッドのshapeオプションで変更できます。 円形、多角形、その他の順に例を示します。

円形

graph = Graph()
graph.node("ellipse", shape="ellipse") # 楕円
graph.node("oval", shape="oval") # 楕円
graph.node("circle", shape="circle") # 正円
graph.node("doublecircle", shape="doublecircle") # 二重円
graph.node("point", shape="point") # 点
graph.node("egg", shape="egg") # 卵型
graph
Loading...

多角形

graph = Graph()
graph.node("box", shape="box") # 長方形
graph.node("square", shape="square") # 正方形
graph.node("triangle", shape="triangle") # 三角形
graph.node("diamond", shape="diamond") # ひし形
graph
Loading...
graph = Graph()
graph.node("pentagon", shape="pentagon") # 五角形
graph.node("hexagon", shape="hexagon") # 六角形
graph.node("septagon", shape="septagon") # 七角形
graph.node("octagon", shape="octagon") # 八角形
graph
Loading...
graph = Graph()
graph.node("trapezium", shape="trapezium") # 四角形
graph.node("parallelogram", shape="parallelogram") # 平行四辺形
graph.node("house", shape="house") # 家の形
graph.node("polygon", shape="polygon", sides="6", skew="0.0", distortion="0.0")
# 多角形。sides: 辺の数、skew: 回転、distortion: 上下の辺の比率
graph
Loading...

その他

graph = Graph()
graph.node("cylinder", shape="cylinder") # 円筒
graph.node("note", shape="note")
graph.node("tab", shape="tab")
graph.node("folder", shape="folder")
graph.node("box3d", shape="box3d")
graph.node("component", shape="component")
graph.node("plaintext", shape="plaintext") # テキストのみ
graph.node("plain", shape="plain") # テキストのみ
graph
Loading...

ノードの色はそれぞれ以下のオプションで指定できます。

  • 枠線: color

  • 背景: fillcolor(ただし、style="filled"の指定も必要)

  • 文字: fontcolor

graph = Graph()
graph.node("node1", color="blue")
graph.node("node2", fillcolor="green", style="filled")
graph.node("node3", fontcolor="red")
graph
Loading...

また、カラースキーム(カラーパレット)による指定も可能です。 Graphvizには多くの種類のカラースキームが用意されています。 node全体のcolorschemeを設定し、各ノードのcolorに色の番号を指定します。 以下はset14というカラースキーム(赤、青、緑、紫)を使用した例です。

graph = Graph()
graph.attr("node", colorscheme="set14")
graph.node("node1", color="1")
graph.node("node2", color="2")
graph.node("node3", color="3")
graph.node("node4", color="4")
graph
Loading...

指定可能な色やカラースキームは以下のページを参照ください。

Color Names - Graphviz

ノードのサイズ

ノードの横幅はwidth, 高さはheightオプションでそれぞれ指定できます。 値は整数または小数で設定可能です。

graph = Graph()
graph.node("width: 1\nheight: 1", width="1", height="1")
graph.node("width: 2\nheight: 1", width="2", height="1")
graph.node("width: 1\nheight: 2", width="1", height="2")
graph.node("width: 2\nheight: 2", width="2", height="2")
graph
Loading...

フォント

ノードのフォントはfontnameで指定可能です。 デフォルトはTimes-Romanです。 環境によって使用可能なフォントは異なりますが、以下はWindowsの日本語環境であれば使用できる可能性は高いと思います。

graph = Graph()
graph.node("Times-Roman", fontname="Times-Roman") # デフォルト
graph.node("Arial", fontname="Arial")
graph.node("Courier New", fontname="Courier New")
graph.node("MS UI Gothic", fontname="MS UI Gothic")
graph.node("Meiryo", fontname="Meiryo")
graph
Loading...

斜体・ボールド体

斜体やボールド体を使用する場合、斜体やボールド体のフォントをfontnameに指定します。

graph = Graph()
graph.node("times-italic", fontname="times-italic")
graph.node("times-bold", fontname="times-bold")
graph
Loading...

ラベルの縦方向位置

ラベルの縦方向位置はlabellocで指定できます。以下の記号のいずれかを指定します。

  • t: 上側 (top)

  • c: 中央 (center)

  • b: 下側 (bottom)

graph = Graph()
graph.node("node-t", labelloc="t")
graph.node("node-c", labelloc="c")
graph.node("node-b", labelloc="b")
graph
Loading...

フォントサイズ

フォントサイズはfontsizeで指定できます。デフォルト値は14です。

graph = Graph()
graph.node("node-20", fontsize="20")
graph.node("node-30", fontsize="30")
graph
Loading...