Draw a tree in Python 3 using graphviz

I think I used Graphviz, a tool that converts text to graphs, from Python3 to draw a tree structure.

What is Graphviz

wikipedia

Graphviz is a tool that converts text into a graph using a unique format called .dot.

sample.dot


digraph {
    node [shape=circle]
    A [label=A]
    B [label=B]
    C [label=C]
        A -> B
        A -> C
}

The following graph can be drawn from the above sample.

sample.png

This time, I think I used this Graphviz from Python3 to draw a tree structure.

Operating environment

Environment

First, install Graphviz using brew

$brew install graphviz
$dot -V
>>> dot - graphviz version 2.38.0 (20140413.2041)

Install graphviz with pip

graphviz is a package that wraps Graphviz so that it can be used from Python.

$pip install graphviz

As a similar package, there is pygraphviz that can be linked with networkx, but this is not compatible with Python3. .. ..

basic operation

Let's write a binary tree as an example.

binary_tree.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from graphviz import Digraph

#format specifies png(Other PDF, PNG,SVG etc. can be specified)
G = Digraph(format='png')
G.attr('node', shape='circle')

N = 15    #Number of nodes

#Add node
for i in range(N):
    G.node(str(i), str(i))
    
#Add edge
for i in range(N):
    if (i - 1) // 2 >= 0:
        G.edge(str((i - 1) // 2), str(i))

# print()Then it will be output in dot format
print(G)

# binary_tree.Save as png
G.render('binary_tree')

Output result

binary_tree.png

reference

Official Example

Recommended Posts

Draw a tree in Python 3 using graphviz
Draw a heart in Python
Draw a scatterplot matrix in python
Draw a CNN diagram in Python
A memo when creating a directed graph using Graphviz in Python
Draw a heart in Python Part 2 (SymPy)
Draw graph in python
Draw a graph of a quadratic function in Python
Create a GIF file using Pillow in Python
View drug reviews using a list in Python
[Python] How to draw a histogram in Matplotlib
Create a MIDI file in Python using pretty_midi
Take a screenshot in Python
Create a data collection bot in Python using Selenium
Create a function in Python
Draw mp3 waveform in Python
Create a dictionary in Python
Draw Poincare's disk in Python
Draw "Draw Ferns Programmatically" in Python
Make a bookmarklet in Python
How to execute a command using subprocess in Python
Translate using googletrans in Python
Using Python mode in Processing
Derivatives learned using Python-(2) Draw a yield curve (JPYLibor curve)-
I made a quick feed reader using feedparser in Python
Try building a neural network in Python without using a library
A memo of writing a basic function in Python using recursion
I tried to make a stopwatch using tkinter in python
Try running a function written in Python using Fn Project
Draw Sine Waves in Blender Python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
GUI programming in Python using Appjar
[python] Manage functions in a list
Hit a command in Python (Windows)
I made a Line-bot using Python!
Create a python GUI using tkinter
Create a DI Container in Python
Draw knots interactively in Plotly (Python)
Drawing a silverstone curve using python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Try using LevelDB in Python (plyvel)
Create a binary file in Python
Compiler in Python: PL / 0 syntax tree
Using global variables in python functions
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Let's see using input in python
Edit videos in Python using MoviePy
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Algorithm (segment tree) in Python (practice)
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Handwriting recognition using KNN in Python
Create a random string in Python
Try using Leap Motion in Python
Schedule a Zoom meeting in Python