github ↗
docs menu

hugin

data and visualization · like Matplotlib

Hugin draws figures from nx tensors. A figure is an immutable value: marks read tensors through channels, scales place the values, and axes and legends follow from the scales. Figures render to PNG, SVG and PDF with a bundled font, and a figure renders to the same bytes every time.

install
opam install hugin
built on
nx

Quick start

Five noisy training curves, one tensor of shape [5; 200], drawn as five lines coloured by run:

ocaml
open Hugin

let steps = Nx.linspace Nx.float64 0. 1. 200

let losses =
  let rates = Nx.create Nx.float64 [| 5; 1 |] [| 2.; 2.5; 3.; 3.5; 4. |] in
  let noise = Nx.Rng.normal ~key:(Nx.Rng.key 0) Nx.float64 [| 5; 200 |] in
  Nx.add (Nx.exp (Nx.neg (Nx.mul rates steps))) (Nx.mul_s noise 0.01)

let () =
  line
    ~x:(num ~title:"progress" steps)
    ~y:(num ~title:"loss" losses)
    ~stroke:(dim ~title:"run" 0)
    ()
  |> title "Five runs"
  |> save "runs.png"

num reads a tensor as quantities and dim 0 reads the index along axis 0 as a category, so each run gets its own colour and the legend lists the runs.

How Hugin works

  • A figure is a value. Hugin.t describes what to draw. Building one reads no data, so a figure can be stored, compared with equal and rendered again.
  • Marks bind channels to roles. dot, line, area, rect, rule, text, image and contour take channels as labelled arguments named after their roles: ~x, ~y, ~fill, ~stroke, ~size and others.
  • Channels broadcast. The channels of a mark broadcast together like nx operands, and each element of the result is one row. A [5; 200] tensor is 1,000 rows of one mark.
  • Scales are found by name. Each channel reads a scale, by default the one named after its role. Marks that read one scale share its domain, and each scale yields one axis or legend.
  • Composition decides sharing. layer draws figures over one another on shared scales, grid arranges them in cells with scales of their own, and the facet channels ~fx and ~fy split one mark into panels.
  • Rendering is three pure stages. resolve fits the scales, layout measures text and places panels, and draw paints a picture. save runs the three and writes a file.

Libraries

Library Module Contents
hugin Hugin Figures, marks, channels, composition, rendering
hugin.kit Hugin_kit Scales, ticks, number and time formats, colour schemes, symbols, curves, statistics
hugin.gg Hugin_gg Points, boxes, affine maps, paths, strokes, colours
hugin.gg.kit Hugin_gg_kit Polygons, and the isolines and isobands of sampled fields
hugin.font Hugin_font OpenType fonts, the bundled Inter faces, glyph runs
hugin.text Hugin_text Rich text and its layout
hugin.vg Hugin_vg Pictures and renderables
hugin.vg.raster Hugin_vg_raster Rendering to pixels and PNG
hugin.vg.svg Hugin_vg_svg Rendering to SVG
hugin.vg.pdf Hugin_vg_pdf Rendering to PDF

Hugin re-exports the modules of the lower libraries that figures use, so most programs link hugin alone.

Next steps

Examples

  • 01-line ↗Lines over a step axis, one per seed, coloured by a dim, in the default theme and in Theme.dark.
  • 02-scatter ↗Dots coloured by category and sized by a quantity, with two legends.
  • 03-bars ↗Bars: a rect over a band scale, its length from zero.
  • 04-heatmap ↗A matrix as cells, its rows and columns read with dim, and a colour bar.
  • 05-facets ↗Small multiples: one panel per category of fx, sharing every scale.
  • 06-confusion-matrix ↗Cells coloured by recall, their counts in a contrasting colour (map_range).
  • 07-attention ↗Attention maps faceted by layer and head, with one colour bar.
  • 08-embedding ↗An embedding of 3,000 dots, and of 100,000 drawn as one image.
  • 09-image-grid ↗Images in wrapped panels, captioned, with the mistakes framed.
  • 10-loss-landscape ↗Filled contours on a log scale and an optimiser's path.
  • 11-dashboard ↗A training dashboard: loss on a log scale over epochs, and validation accuracy, sharing x.
  • 12-paper-figure ↗A two-column paper figure at 8 points, panels labelled (a) to (d).
  • 13-histogram ↗Two histograms over shared bins from Stats.histogram, drawn as rects from their edges.
  • 14-area ↗A band between the lowest and highest of eight seeds (area with y2), under their mean.
  • 15-errorbars ↗Error bars as a composition: a rule from y to y2 under each dot.
  • 16-styles ↗Series told apart by colour and dash from one scale, a dotted rule, a frame and a legend inside the panel.
  • 17-calibration ↗A calibration plot over the dashed diagonal abline y = x, its axes in percent.

this page is tested with mdxedit this page ↗