scatterplot - JosephCottam/Stencil GitHub Wiki

Scater Plots

Scater plots are a fundamental visualization technique. This page explores the creation of scatterplots through progressively more involved stencil programs.

These examples were created under the bokeh runtime.

Read and Plot (and what gets generated from context)

The simplest scatter plot is to load data into a table that looks like a scatter plot schema (x,y,color) and have a render policy on that table with the bindings automatically generated.

(stencil SimpleScatter
   (table data 
     (fielsd x y color)
     (data (file "./dataset.csv"))
     (render scatter (bind auto))))

The above example takes advantage of context in two ways in the nested render context. Making it an un-nested render context and explicit binding yields the following.

(stencil SimpleScatter
   (view main dataScatter)
   (render dataScatter data scatter (bind (x:x) (y:y) (color:color)))
   (table data
      (fields x y color)
      (data (file "./dataset.csv"))))

The original simple program also omitted a view statement, which may collect multiple renderings. An automatically generated view contains all of the renderers defined in a program. For convenience, we have used table-field names that match the scatter plot schema field names. However, with explicit binding control, this need not be the case.

(stencil SimpleScatter
    (view main dataScatter)
    (render dataScatter data scatter (bind (x:a) (y:b) (color:c)))
    (table data
       (fields a b c)
       (data (file "./dataset.csv"))))

Transform

Stencil programs can include transformations of source data. Our previous examples could have included this directly on read with a more complicated data policy. More common is to chain tables together.

(stencil  TwoTabled
   (table source (fields a b c) (data (file "./dataset.csv")))
   (table transformed
      (fields m n o)
      (data (pull source
              (let (m : (interpolate a 1 1000 0 100))
                   (n : (interpolate b 0 1 0 100))
                   (o : (color c)))))
      (render scatter (bind (x:m) (y:n) (c:o)))))

A table may depend on another table by defining a data policy with a pull declaration. The pull declaration must inidicate a table to get data from (the table named "source," in this case) In addition to a source table, a pull also includes a transformation. The transformation must produce tuples to store in the current table, so the results must include all of the fields of the current table. Tuple producers are tuple, ptuple, tuples and ptuples. The tuple production can be infered in a let statement by simply binding the fields of the let.

(stencil  TwoTabled
   (table source (fields a b c) (data (file "./dataset.csv")))
   (table transformed
      (fields m n o)
      (data (pull source
              (let (m : (interpolate a 1 1000 0 100))
                   (n : (interpolate b 0 1 0 100))
                   (o : (color c))
                (ptuple (fields m n o) m n o))))
      (render scatter (bind (x:m) (y:n) (c:o)))))

Assuming (interpolate <value> <min-in> <max-in> <min-out> <max-out>) exists, the the transformed table is a reprojection of the source table into a 0..100 range.

⚠️ **GitHub.com Fallback** ⚠️