2  Quickstart

A minimal introduction to Napkinsketch

Setup

Add Napkinsketch to your deps.edn:

{:deps {org.scicloj/napkinsketch {:mvn/version "..."}}}

Then require the API:

(ns napkinsketch-book.quickstart
  (:require
   ;; Tablecloth β€” dataset manipulation
   [tablecloth.api :as tc]
   ;; Kindly β€” notebook rendering protocol
   [scicloj.kindly.v4.kind :as kind]
   ;; Napkinsketch β€” composable plotting
   [scicloj.napkinsketch.api :as sk]))

Use Clay or other Kindly-compatible tools to visualize the examples below.

Your First Plot

Load the classic iris dataset and scatter two columns. That is all it takes β€” one def and one pipeline:

(def iris (tc/dataset "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
                      {:key-fn keyword}))
(-> iris
    (sk/lay-point :sepal_length :sepal_width))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5
  • tc/dataset loads a CSV into a tech.ml.dataset dataset (which we typically use through Tablecloth).

  • The :key-fn keyword option converts the CSV header strings to Clojure keywords, which is conventional.

  • sk/lay-point adds a scatter layer β€” each row becomes a dot.

Plain Data

You do not need to load a CSV β€” Napkinsketch accepts plain Clojure data and coerces it into a dataset internally. A map of columns works directly:

(-> {:x [1 2 3 4 5] :y [2 4 3 5 4]}
    (sk/lay-point :x :y))
yx1.01.52.02.53.03.54.04.55.02.02.53.03.54.04.55.0

When the dataset has few columns, you can skip the column names β€” napkinsketch infers them from the dataset shape:

(-> {:x [1 2 3 4 5] :y [2 4 3 5 4]}
    sk/lay-point)
yx1.01.52.02.53.03.54.04.55.02.02.53.03.54.04.55.0

See Core Concepts for more input formats.

String column names also work β€” keywords are conventional but not required:

(-> {"x" [1 2 3 4 5] "y" [2 4 3 5 4]}
    (sk/lay-point "x" "y"))
yx1.01.52.02.53.03.54.04.55.02.02.53.03.54.04.55.0

Color

Bind :color to a column to color points by group.

(-> iris
    (sk/lay-point :sepal_length :sepal_width {:color :species}))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

More Chart Types

Each sk/lay-* function adds a different chart type.

Histogram β€” pass a single column for automatic binning:

(-> iris
    (sk/lay-histogram :sepal_length))
sepal length4.55.05.56.06.57.07.58.00510152025

Bar chart β€” count occurrences of a categorical column:

(-> iris
    (sk/lay-bar :species))
speciessetosaversicolorvirginica05101520253035404550

Horizontal bars β€” flip with sk/coord:

(-> iris
    (sk/lay-bar :species)
    (sk/coord :flip))
species05101520253035404550setosaversicolorvirginica

Line chart β€” connect points in order:

(-> {:x [1 2 3 4 5 6 7 8]
     :y [3 5 4 7 6 8 7 9]}
    (sk/lay-line :x :y))
yx123456783456789

Boxplot β€” compare distributions across categories:

(-> iris
    (sk/lay-boxplot :species :sepal_width))
sepal widthspeciessetosaversicolorvirginica2.02.53.03.54.04.5

See the Methods chapter for the full list of 25 chart types.

Inference

sk/view can pick the chart type for you based on column types. Two numerical columns produce a scatter plot:

(-> iris
    (sk/view :sepal_length :sepal_width))
sepal widthsepal length4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

A single categorical column produces a bar chart:

(-> iris
    (sk/view :species))
speciessetosaversicolorvirginica05101520253035404550

A single numerical column produces a histogram:

(-> iris
    (sk/view :sepal_length))
sepal length4.55.05.56.06.57.07.58.00510152025

See the Inference Rules chapter for the full set of rules.

Multiple Layers

Use sk/view to set shared column mappings, then add layers with sk/lay-*. All layers inherit the view’s mappings. Here sk/lay-lm adds a linear model (regression line) per group:

(-> iris
    (sk/view :sepal_length :sepal_width {:color :species})
    sk/lay-point
    sk/lay-lm)
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5

Titles and Labels

Use sk/options for width, height, title, and axis labels:

(-> iris
    (sk/lay-point :petal_length :petal_width {:color :species})
    (sk/options {:width 500 :height 350
                 :title "Iris Petals"
                 :x-label "Petal Length (cm)"
                 :y-label "Petal Width (cm)"}))
Iris PetalsPetal Width (cm)Petal Length (cm)speciessetosaversicolorvirginica12345670.00.51.01.52.02.5

Dashboards

Combine multiple plots with sk/arrange:

(sk/arrange [(sk/lay-point iris :sepal_length :sepal_width {:color :species})
             (sk/lay-histogram iris :sepal_length {:color :species})]
            {:cols 2})
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.02.02.53.03.54.04.5sepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.00246810121416

Export

Save a plot to SVG with sk/save:

(-> iris
    (sk/lay-point :sepal_length :sepal_width)
    (sk/save "my-plot.svg"))

What’s Next

  • Composable Plotting β€” how sketches, views, and layers compose
  • Core Concepts β€” data formats, marks, stats, color, grouping, coordinates
  • Scatter Plots β€” the most common starting point for chart types
  • Cookbook β€” recipes for common multi-layer plots
source: notebooks/napkinsketch_book/quickstart.clj