Skip to content

Instantly share code, notes, and snippets.

@banditkings
Last active December 29, 2023 22:36
Show Gist options
  • Save banditkings/d7c5c2d1ce42b5cf324b62258dfd515b to your computer and use it in GitHub Desktop.
Save banditkings/d7c5c2d1ce42b5cf324b62258dfd515b to your computer and use it in GitHub Desktop.
Draw a DAG with `graphviz` python api
# Simple example of using graphviz to draw DAGs while working in a jupyter notebook environment
import graphviz as gr
# Simple DAG with a confounder
h = gr.Digraph('Simple DAG with confounder')
h.edge('Treatment', 'Outcome')
h.edge('Confounder', 'Treatment')
h.edge('Confounder', 'Outcome')
display(h)
# Regression Example
g = gr.Digraph('Regression with subgraph')
g.edge('alpha', 'mu')
g.edge('beta', 'mu')
#subgraph name must begin with 'cluster'
with g.subgraph(name='cluster') as c:
c.attr(label='N', labeljust='r', labelloc='b')
c.edges([('mu', 'y'), ('sigma', 'y')])
c.node('y', label='obs', style='filled')
display(g)
@banditkings
Copy link
Author

'hello world' examples of using the python graphviz library to create simple DAGs while in jupyter notebook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment