Skip to content

sglduck

sglduck implements the SGL graphics language for Python. SGL is a grammar-of-graphics language designed to look and feel like SQL. sglduck targets DuckDB as its database and Lets-Plot as its rendering backend.

Installation

pip install sglduck

Usage

db_get_plot is the primary interface: it takes a DuckDB connection and a SGL statement and returns the corresponding plot. The package bundles a few example datasets (sglduck.data) to plot against.

import duckdb
import sglduck

con = duckdb.connect()
con.register("cars", sglduck.data.cars())

plot = sglduck.db_get_plot(
    con,
    """
    visualize
        horsepower as x,
        miles_per_gallon as y
    from cars
    using points
    """,
)

# In a script, use plot.show() (opens a browser) or plot.save("plot.svg");
# in a Jupyter notebook the plot displays inline. This page embeds its SVG:
print(plot.to_svg())
40 60 80 100 120 140 160 180 200 220 10 15 20 25 30 35 40 45 miles_per_gallon horsepower

Next steps

  • Get started — a tour of the SGL language and db_get_plot.
  • Example gallery — a collection of plots generated with sglduck.
  • Reference — the public Python API.
  • SGL paper — the language in depth, including its underlying grammar of graphics.