Skip to content

Visualize

Basic plot

The function plot() plots the graph using matplotlib and networkx in the background. The position of the nodes can be set using a dictionary; otherwise, the positions are calculated automatically. If the type of nodes (Exposure, Outcome, .etc) were set (see Creating DAGs), by default the plot includes a legend with the node types. There are many options for customization. Check the documentation of plot() for a comprehensive list of options.

from causalinf import scm
dag = """
Y <- {X1, X2, D}
D <- {X1, Z}
Z <- X2
"""
pos = {'D': (0, 0),
       'Y': (1, 0),
       'Z': (-1, 0),
       'X2': (0.5, -1),
       'X1': (0.5, 1)}
var_types = {"Exposure":"Y", "Outcome":"D"}
G = scm.DAG(dag, nodes_role=var_types, nodes_position=pos)
G.plot()

Bidirected edges are represented by default in the plot with dashed curved lines. In the context of causal inference with DAGs, by convention these arrows (often called "arcs") indicate a latent or unobserved common cause between the variables connected by the bidirected arrow. Alternatively, these variables can be included explicitly when creating the DAG.

Undirected edges, on the other hand, in the context of causal inference with DAGs, are used to represent the skeleton of the DAG or observationally equivalent edges. That is, they are edges whose direction cannot be decided inferentially using observational data unless other parametric assumptions are adopted.

Here is an example (the example is just for illustration; the types of edges have no meaning in the example):

from causalinf import scm
dag = """
Y <- {X1, X2, D}
D <- X1
Z <- X2
D -- Z
Y <-> X2
"""
pos = {'D': (0, 0),
       'Y': (1, 0),
       'Z': (-1, 0),
       'X2': (0.5, -1),
       'X1': (0.5, 1)}
var_types = {"Exposure":"Y", "Outcome":"D"}
G = scm.DAG(dag, nodes_role=var_types, nodes_position=pos)
G.plot()

Graph Styles

Default

There are some default styles provided by the package.

from causalinf import scm
dag = """
Y <- {X1, X2, D}
D <- {X1, Z}
Z <- X2
"""
pos = {'D': (0, 0),
       'Y': (1, 0),
       'Z': (-1, 0),
       'X2': (0.5, -1),
       'X1': (0.5, 1)}
var_types = {"Exposure":"Y", "Outcome":"D"}
G = scm.DAG(dag, nodes_role=var_types, nodes_position=pos)
G.plot() # this is the default style, equivalent to G.plot(graph_style="default")

Rectangular

Retangular style gives more space for labels. But see also Node style.

G.plot(graph_style="rectangle", nodes_label={'D':'Treatment'}) 

Pearl

The "pearl" style uses the design adopted in Pearl (2009). The position of the node label needs to be adjusted manually. The label positions can be adjusted in block using a float or individually using a dictionary. For instance, node_label_adj_y={"Z":.1} adjusts only the y position of node , while node_label_adj_y=.1 adjusts it for all nodes.

G.plot(graph_style="pearl", node_label_adj_y=.1) 

[TODO]{.todo .TODO} Plot sub-DAG

[TODO]{.todo .TODO} Nodes

Node label

It is possible to use labels for the nodes. Labels accept LaTeX mathematical expressions. For instance, to use subscripts for some variables, we can set the labels as follows:

from causalinf import scm
dag = """
Y <- {X1, X2, D}
D <- {X1, Z}
Z <- X2
"""
pos = {'D': (0, 0),
       'Y': (1, 0),
       'Z': (-1, 0),
       'X2': (0.5, -1),
       'X1': (0.5, 1)}
labels = {
    "X2" : "$X_2$",
    "X1" : "$X_1$"
}
var_types = {"Exposure":"Y", "Outcome":"D"}

G = scm.DAG(dag, nodes_role=var_types, nodes_label=labels, nodes_position=pos)
G.plot()

[TODO]{.todo .TODO} Node style

[TODO]{.todo .TODO} Edges

Edge style

References

  • Pearl, J. (2009). Causality: Models, Reasoning and Inference. : Cambridge University Press.