Nanuq dev log ยท 27 October 2026

Plotting with ggplotnim

Query in Polars, plot in Nim.

Walkthrough ยท Nanuq 0.1 ยท built with Nanuq 0.6.1, Polars 2.1.0 and Nim 2.2.4 ยท rendered with nimib

How do we plot a Nanuq DataFrame? The obvious candidate in Nim is ggplotnim, a grammar of graphics library modelled on R's ggplot2. This post shows how to hand a Nanuq DataFrame to it, and then looks at where a smoother bridge could go. It follows How Nanuq works.

What ggplotnim takes as inputโ†‘ contents

ggplotnim has a single entry point, ggplot(data: DataFrame, aes), and the DataFrame there is the one from Datamancer, a pure Nim dataframe library that ggplotnim depends on. So the bridge is a conversion: the optional module nanuq/datamancer has toDatamancer and fromDatamancer, in the spirit of Python Polars' to_pandas and from_pandas. Nanuq itself doesn't depend on Datamancer; only that module does.

Importing bothโ†‘ contents

Both libraries have a DataFrame and a readCsv, so with both imported Nim asks us to pick when we call readCsv: qualifying it as nanuq.readCsv is enough. Everything else (col, filter, ggplot, aes) resolves by type. Here we don't even need readCsv, since we use the Palmer penguins data bundled in nanuq/datasets.

import nanuq, nanuq/[datasets, datamancer]
import ggplotnim
let penguins = penguins()
echo penguins.shape
(344, 8)

From Nanuq to Datamancerโ†‘ contents

toDatamancer copies every column, keeping names, order and types: integers become int columns, floats float, strings string. Underneath it is one toList per column, each a single call across the C ABI.

Two penguins have no measurements. A column with nulls would arrive as a Datamancer object column with null entries, which ggplotnim plots less happily than a typed column, so we drop those rows in Polars first.

let points = penguins.dropNulls("flipper_length_mm", "body_mass_g").toDatamancer
echo points.head(3)
DataFrame with 8 columns and 3 rows:
     Idx              species               island       bill_length_mm        bill_depth_mm    flipper_length_mm          body_mass_g                  sex                 year
  dtype:               string               string                float                float                  int                  int               object                  int
       0               Adelie            Torgersen                 39.1                 18.7                  181                 3750                 male                 2007
       1               Adelie            Torgersen                 39.5                 17.4                  186                 3800               female                 2007
       2               Adelie            Torgersen                 40.3                   18                  195                 3250               female                 2007

A scatter plotโ†‘ contents

ggplot(points, aes("flipper_length_mm", "body_mass_g", color = "species")) +
  geom_point(size = 2.0) +
  ggtitle("Palmer penguins: body mass vs flipper length") +
  ggsave("blog/2026-10-27-penguins-scatter.png", width = 640, height = 420)
Scatter plot of body mass against flipper length, coloured by species
Body mass against flipper length, coloured by species.

Aggregate in Polars, plot the resultโ†‘ contents

Most plots don't need every row. The heavy work can stay in Polars, and only the small result crosses over to Datamancer. Here Polars computes the mean body mass per species (the mean skips nulls), and three rows are converted.

let means = penguins.lazy
  .groupBy(col("species"))
  .agg(col("body_mass_g").mean.alias("mean_body_mass_g"))
  .sort("species")
  .collect
echo means

ggplot(means.toDatamancer, aes("species", "mean_body_mass_g", fill = "species")) +
  geom_bar(stat = "identity", position = "identity") +
  ggtitle("Mean body mass per species (computed by Polars)") +
  ggsave("blog/2026-10-27-penguins-means.png", width = 640, height = 420)
shape: (3, 2)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ species   โ”† mean_body_mass_g โ”‚
โ”‚ ---       โ”† ---              โ”‚
โ”‚ str       โ”† f64              โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ Adelie    โ”† 3700.662252      โ”‚
โ”‚ Chinstrap โ”† 3733.088235      โ”‚
โ”‚ Gentoo    โ”† 5076.01626       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Bar chart of mean body mass for Adelie, Chinstrap and Gentoo
Mean body mass per species, aggregated by Polars before plotting.

What this costsโ†‘ contents

The conversion copies: every column goes from Polars' Arrow buffers to a Nim seq, then into a Datamancer column. For a plot of a few thousand points this doesn't matter, and for bigger data the second example is the pattern to follow: aggregate in Polars, convert the small result.

Exploring: where this could goโ†‘ contents

This is the start of an exploration, not a plan. It is tracked in the backlog under Ecosystem. Three directions, from the smallest to the most ambitious:

  1. A ggplot overload for Nanuq DataFrames. ggplot(df: nanuq.DataFrame, aes(...)) could convert only the columns named in aes, saving the explicit toDatamancer and the copies of unused columns.
  2. Zero-copy columns. Once columns cross through the Arrow C Data Interface, numeric Polars buffers could be wrapped without copying. Only worth it for very large plots.
  3. A Narwhals-style dataframe API. Python's Narwhals lets libraries accept pandas, Polars and others through one Polars-like interface. A Nim equivalent would be a small common layer, implemented by both Nanuq and Datamancer, that ggplotnim (or any library) could accept directly. Datamancer is already generic over a ColumnLike concept, which is a promising hook.

Open questions for the exploration: