A memo when creating a directed graph using Graphviz in Python

A memo when creating a directed graph using Graphviz in Python

Operating environment

Environment

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

Actually try

First of all, the simple one.

Code example
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()
output

output.png

Code description
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. output.png

Change the appearance of the graph

You can change the shape and color of the node.

Shape change

Code example
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()
output

output2.png

Code description
#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.

Color change

Code example
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()
output

output3.png

Code description
#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.

reference

Draw a beautiful graph using Graphviz on Python

Recommended Posts

A memo when creating a directed graph using Graphviz in Python
Draw a tree in Python 3 using graphviz
A memo when creating a python environment with miniconda
Precautions when using pit in Python
[Python] Creating a scraping tool Memo
Precautions when creating a Python generator
A memo of writing a basic function in Python using recursion
When creating a matrix in a list
When using regular expressions in Python
When writing a program in Python
I get a can't set attribute when using @property in python
Scraping a website using JavaScript in Python
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
[Python memo] Be careful when creating a two-dimensional array (list of lists)
A memo when setting up a Docker container for using JUMAN ++, KNP, python
A useful note when using Python for the first time in a while
Draw a graph of a quadratic function in Python
Create a GIF file using Pillow in Python
Ubuntu18.04.05 Creating a python virtual environment in LTS
A memo that I wrote a quicksort in Python
Creating numbering process using python in DynamoDB Local Numbering process
Create a standard normal distribution graph in Python
[Python] Draw a directed graph with Dash Cytoscape
A memo about writing merge sort in Python
[Memo] I tried a pivot table in Python
View drug reviews using a list in Python
Create a MIDI file in Python using pretty_midi
A memo for creating a python environment by a beginner
NetworkX memo (directed graph)
Graph drawing in python
Draw graph in python
[Grasshopper] When creating a data tree on Python script
Create a data collection bot in Python using Selenium
Initial settings when using the foursquare API in python
Until drawing a 3D graph in Python on windows10
A memorandum when writing experimental code ~ Logging in python
Problems when creating a csv-json conversion tool with python
Things to note when initializing a list in Python
What's in that variable (when running a Python script)
Use communicate () when receiving output in a Python subprocess
How to exit when using Python in Terminal (Mac)
How to execute a command using subprocess in Python
Minimum memo when using Python on Mac (pyenv edition)
Memo for building a machine learning environment using Python
Ssh connection memo using ProxyCommand of ssh_config in Python
A memo when checking whether the specified key exists in the defined dictionary with python
Try creating a Deep Zoom file format .DZI in Python
Create a function in Python
Create a dictionary in Python
A memo when face is detected with Python + OpenCV quickly
I made a quick feed reader using feedparser in Python
A story that stumbled when using pip in a proxy environment
How to develop in a virtual environment of Python [Memo]
Make a bookmarklet in Python
To return char * in a callback function using ctypes in Python
Python memo using perl --join
Attention when os.mkdir in Python
Try building a neural network in Python without using a library
Solve the Japanese problem when using the CSV module in Python.
Graph time series data in Python using pandas and matplotlib
Things to keep in mind when using Python with AtCoder