Graphviz - nokia/PyBGL GitHub Wiki
Overview
Graphviz is a well-known tool dedicated to export graph in various formats (e.g. svg, jpg, etc.). If you are working in a Jupyter ipython notebook, it is possible to display Graphviz results directly in your Jupyter notebook using the gvmagic
extension.
Note that graphviz provides several engine to compute how to place the vertices of your graph in the output image. For small graphs, you should use dot
. For large graphs, prefer fdp
which is significantly faster.
Graphviz
Installation
- Debian / Ubuntu
sudo apt update
sudo apt install graphviz
Usage
See man dot
.
Jupyter notebook
Installation
- Windows: Install anaconda
conda install -c conda-forge jupyter_contrib_nbextensions
- Debian / Ubuntu
sudo apt update
sudo apt install jupyter-notebook
- Under Linux, anaconda-based installation may also be considered.
Usage
- Anaconda installation
jupyter notebook
- Debian / Ubuntu
jupyter-notebook
Drawing graphs
- Minimal example (in a Jupyter notebook):
from pybgl.graph import *
from pybgl.ipynb import ipynb_display_graph
g = DirectedGraph(2)
add_edge(0, 1, g)
print(g.to_dot()) # Print the Graphviz description of the graph
ipynb_display_graph(g) # Render the graph in Jupyter using Graphviz
- If you use dark theme, it is recommended to change the default foreground color:
from pybgl.graphviz import GraphvizStyle
GraphvizStyle.set_fg_color("grey")
- Note that
ipynb_display_graph
and.to_dot
support optional parameter to customize the graph rendering (see e.g.automaton.py
):dv
: default vertex style;de
: default edge style;dg
: graph style;dpv
: vertex-dependant style;dpe
: edge-dependant style;extra_style
: extra style (e.g.splines
).
gvmagic
gvmagic
is a jupyter notebook extension enabling some magics like %dotstr
that may be handy in some situations.
Installation
- Anaconda
sudo jupyter nbextension install https://raw.github.com/cjdrake/ipython-magic/master/gvmagic.py
jupyter nbextension list
mkdir -p ~/.ipython/extensions/
ln -s /usr/local/share/jupyter/nbextensions/gvmagic.py ~/.ipython/extensions/gvmagic.py
- Debian / Ubuntu
sudo jupyter-nbextension install https://raw.github.com/cjdrake/ipython-magic/master/gvmagic.py
jupyter-nbextension list
mkdir -p ~/.ipython/extensions
ln -s /usr/local/share/jupyter/nbextensions/gvmagic.py ~/.ipython/extensions/gvmagic.py
Usage
In your notebook, execute a code cell containing:
%reload_ext gvmagic
%dotstr 'digraph G {0 -> 1}'