GraphViz + Ibis

Ibis supports visualizing an expression as a directed graph using GraphViz.

To get started, make sure you’ve installed the necessary dependencies.

$ pip install 'ibis-framework[duckdb,examples,visualization]'

For instance, you can replace duckdb with snowflake if you want to use the Snowflake backend.

Let’s run through a few examples.

First we’ll import the things we need.

import ibis

from ibis import _
from ibis.expr.visualize import to_graph

Now we can visualize an expression graph.

Here’s a call to select.

t = ibis.examples.penguins.fetch()
expr = t.select(lowered=_.species.lower())
to_graph(expr)

The way to read the graph is from top to bottom.

Let’s look at a more complex example: group_by.

expr = (
    t.group_by(_.species)
    .agg(
        bill_depth_avg=_.bill_depth_mm.mean(),
        bill_length_avg=_.bill_length_mm.mean(),
    )
)
to_graph(expr)

Switching gears, let’s look at a join and show customization of node and edge attributes.

left = ibis.table(dict(a="int64", b="string"), name="left")
right = ibis.table(dict(b="string", c="int64", d="string"), name="right")
expr = (
    left.inner_join(right, "b")
    .select(left.a, b=right.c, c=right.d)
    .mutate(arrays=ibis.array([1, 2, 3]))
)
to_graph(
    expr,
    label_edges=True,
    node_attr={"shape": "hexagon", "color": "green", "fontname": "Roboto Mono"},
    edge_attr={"fontsize": "12", "fontname": "Comic Sans MS"}
)

Please try out to_graph and give us feedback on Zulip or in a GitHub issue!

Back to top