A memo when creating a directed graph using Graphviz in Python
First, install Graphviz on Windows.
https://graphviz.gitlab.io/_pages/Download/Download_windows.html
This time, I downloaded the msi. After downloading, execute it and install it according to the wizard. After installation, pass it through the path.
Next, install graphviz with pip.
pip install graphviz
First of all, the simple one.
from graphviz import Digraph
graph = Digraph(format="png")
#Add node
graph.node("node1")
graph.node("node2")
graph.node("node3")
graph.node("node4")
#Add edge
graph.edge("node1", "node2")
graph.edge("node1", "node3")
graph.edge("node2", "node4")
graph.edge("node3", "node4")
#Save image
#No extension needed
graph.render("image/output")
#Display image
graph.view()
from graphviz import Digraph
graph = Digraph(format="png")
Import Digraph to create a directed graph. In addition to png, pdf and svg can also be output.
#Add node
graph.node("node1")
graph.node("node2")
graph.node("node3")
graph.node("node4")
Create a node. The given argument is drawn inside the node.
#Add edge
graph.edge("node1", "node2")
graph.edge("node1", "node3")
graph.edge("node2", "node4")
graph.edge("node3", "node4")
Create an edge. An arrow is attached in the direction of the first argument → the second argument.
As shown below, a new node will be created if you specify it here even if you have not created a node.
#Add edge
graph.edge("node1", "node2")
graph.edge("node1", "node3")
graph.edge("node2", "node4")
graph.edge("node3", "node4")
#Specify a node that has not been created
graph.edge("A", "B")
The output is as follows.
You can change the shape and color of the node.
from graphviz import Digraph
graph = Digraph(format="png")
#Add node
graph.attr("node", shape="square") #Make the shape square
graph.node("node1")
graph.node("node2")
graph.attr("node", shape="star") #Make the shape a star
graph.node("node3")
graph.node("node4")
graph.node("node5", shape="circle") #Can be set individually
graph.node("node6")
#Add edge
graph.edge("node1", "node2")
graph.edge("node1", "node3")
graph.edge("node2", "node4")
graph.edge("node3", "node4")
graph.edge("node4", "node5")
graph.edge("node4", "node6")
#Save image
#No extension needed
graph.render("image/output2")
#Display image
graph.view()
#Add node
graph.attr("node", shape="square") #Make the shape square
graph.node("node1")
graph.node("node2")
graph.attr("node", shape="star") #Make the shape a star
graph.node("node3")
graph.node("node4")
graph.node("node5", shape="circle") #Can be set individually
graph.node("node6")
You can change the settings of all nodes using the attr method.
You can specify the shape by setting shape =" (shape) "
.
If you specify the shape when creating a node, you can change only the settings of that node.
from graphviz import Digraph
graph = Digraph(format="png")
#Appearance settings
graph.attr("node", style="filled", fillcolor="black", color="red") #Node color settings
graph.attr("edge", color="cyan") #Edge color setting
#Add node
graph.node("node1", style="filled", fillcolor="palegreen", fontcolor="blue")
graph.node("node2", style="filled", fillcolor="yellow")
graph.node("node3", fontcolor="magenta")
graph.node("node4", style="filled", fillcolor="#808080")
graph.node("node5", fontcolor="white")
#Add edge
graph.edge("node1", "node2")
graph.edge("node1", "node3")
graph.edge("node2", "node4")
graph.edge("node3", "node4")
graph.edge("node3", "node5")
#Save image
#No extension needed
graph.render("image/output3")
#Display image
graph.view()
#Appearance settings
graph.attr("node", style="filled", fillcolor="black", color="red") #Node color settings
graph.attr("edge", color="cyan") #Edge color setting
#Add node
graph.node("node1", style="filled", fillcolor="palegreen", fontcolor="blue")
graph.node("node2", style="filled", fillcolor="yellow")
graph.node("node3", fontcolor="magenta")
graph.node("node4", style="filled", fillcolor="#808080")
graph.node("node5", fontcolor="white")
The whole setting can be done by using the attr method, like when changing the shape. Nodes and edges can be set by setting the first argument to " node "
or " edge "
.
To fill it, use style =" filled "
and fill color =" (color) "
.
To change the text color, use font color =" (color) "
.
The color can also be specified by the color code.
It can also be set individually when creating a node.
Draw a beautiful graph using Graphviz on Python
Recommended Posts