2  Render

Building and drawing a plot: plot/build (the built plot, data only), plot/render (a clojure2d canvas), plot/render-image (a java.awt.image.BufferedImage), plot/cvd-image (an image as seen with a colour vision deficiency) and plot/save (an image or canvas to a file). The first three take the same render options, as their second argument or under the plot’s :options (merged under the argument).

(ns reference.render
  (:require [cljplot2.chart :as chart]
            [cljplot2.core :as plot]
            [cljplot2.describe :as describe]
            [cljplot2.pipeline :as pipeline]
            [notebook-utils :as nu]))

Data: Edgar Anderson’s iris measurements, R datasets package iris (Anderson 1935; Fisher 1936); licence: Part of R (GPL-2 | GPL-3).

(def iris (nu/dataset :iris))
sepal-length sepal-width petal-length petal-width species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa
5 3.4 1.5 0.2 setosa
4.4 2.9 1.4 0.2 setosa
4.9 3.1 1.5 0.1 setosa
5.4 3.7 1.5 0.2 setosa
4.8 3.4 1.6 0.2 setosa
4.8 3 1.4 0.1 setosa
4.3 3 1.1 0.1 setosa
5.8 4 1.2 0.2 setosa
5.7 4.4 1.5 0.4 setosa
5.4 3.9 1.3 0.4 setosa
5.1 3.5 1.4 0.3 setosa
5.7 3.8 1.7 0.3 setosa
5.1 3.8 1.5 0.3 setosa
5.4 3.4 1.7 0.2 setosa
5.1 3.7 1.5 0.4 setosa
4.6 3.6 1 0.2 setosa
5.1 3.3 1.7 0.5 setosa
4.8 3.4 1.9 0.2 setosa
5 3 1.6 0.2 setosa
5 3.4 1.6 0.4 setosa
5.2 3.5 1.5 0.2 setosa
5.2 3.4 1.4 0.2 setosa
4.7 3.2 1.6 0.2 setosa
4.8 3.1 1.6 0.2 setosa
5.4 3.4 1.5 0.4 setosa
5.2 4.1 1.5 0.1 setosa
5.5 4.2 1.4 0.2 setosa
4.9 3.1 1.5 0.2 setosa
5 3.2 1.2 0.2 setosa
5.5 3.5 1.3 0.2 setosa
4.9 3.6 1.4 0.1 setosa
4.4 3 1.3 0.2 setosa
5.1 3.4 1.5 0.2 setosa
5 3.5 1.3 0.3 setosa
4.5 2.3 1.3 0.3 setosa
4.4 3.2 1.3 0.2 setosa
5 3.5 1.6 0.6 setosa
5.1 3.8 1.9 0.4 setosa
4.8 3 1.4 0.3 setosa
5.1 3.8 1.6 0.2 setosa
4.6 3.2 1.4 0.2 setosa
5.3 3.7 1.5 0.2 setosa
5 3.3 1.4 0.2 setosa
7 3.2 4.7 1.4 versicolor
6.4 3.2 4.5 1.5 versicolor
6.9 3.1 4.9 1.5 versicolor
5.5 2.3 4 1.3 versicolor
6.5 2.8 4.6 1.5 versicolor
5.7 2.8 4.5 1.3 versicolor
6.3 3.3 4.7 1.6 versicolor
4.9 2.4 3.3 1 versicolor
6.6 2.9 4.6 1.3 versicolor
5.2 2.7 3.9 1.4 versicolor
5 2 3.5 1 versicolor
5.9 3 4.2 1.5 versicolor
6 2.2 4 1 versicolor
6.1 2.9 4.7 1.4 versicolor
5.6 2.9 3.6 1.3 versicolor
6.7 3.1 4.4 1.4 versicolor
5.6 3 4.5 1.5 versicolor
5.8 2.7 4.1 1 versicolor
6.2 2.2 4.5 1.5 versicolor
5.6 2.5 3.9 1.1 versicolor
5.9 3.2 4.8 1.8 versicolor
6.1 2.8 4 1.3 versicolor
6.3 2.5 4.9 1.5 versicolor
6.1 2.8 4.7 1.2 versicolor
6.4 2.9 4.3 1.3 versicolor
6.6 3 4.4 1.4 versicolor
6.8 2.8 4.8 1.4 versicolor
6.7 3 5 1.7 versicolor
6 2.9 4.5 1.5 versicolor
5.7 2.6 3.5 1 versicolor
5.5 2.4 3.8 1.1 versicolor
5.5 2.4 3.7 1 versicolor
5.8 2.7 3.9 1.2 versicolor
6 2.7 5.1 1.6 versicolor
5.4 3 4.5 1.5 versicolor
6 3.4 4.5 1.6 versicolor
6.7 3.1 4.7 1.5 versicolor
6.3 2.3 4.4 1.3 versicolor
5.6 3 4.1 1.3 versicolor
5.5 2.5 4 1.3 versicolor
5.5 2.6 4.4 1.2 versicolor
6.1 3 4.6 1.4 versicolor
5.8 2.6 4 1.2 versicolor
5 2.3 3.3 1 versicolor
5.6 2.7 4.2 1.3 versicolor
5.7 3 4.2 1.2 versicolor
5.7 2.9 4.2 1.3 versicolor
6.2 2.9 4.3 1.3 versicolor
5.1 2.5 3 1.1 versicolor
5.7 2.8 4.1 1.3 versicolor
… … … … …
(def iris-plot
  {:data iris
   :mapping {:x :sepal-length :y :petal-length :color :species}
   :charts [(chart/scatter)]})

2.1 Size and quality

The image size in pixels - the whole image, legends and titles included - and the rendering quality (clojure2d hints): :low draws without antialiasing (faster, jagged edges), :mid antialiased for speed, :high antialiased for quality, :highest adds exact stroke geometry (lines can look softer).

Option Type Default Description
:width a positive integer 600 target width in pixels
:height a positive integer 600 target height in pixels
:hint one of :high :highest :low :mid :high rendering quality hint (antialiasing and text metrics)
(plot/render-image iris-plot {:width 500 :height 300 :hint :low})

… against :highest - smooth edges and text:

(plot/render-image iris-plot {:width 500 :height 300 :hint :highest})

2.2 Inspection

Two aids for finding out why a plot looks as it does. :debug true paints every layout item’s box under its content - panel, axes, axis titles, legend:

Option Type Default Description
:debug true or false or nil false paint every layout item's box (a distinct colour, its outline and id) under its content
:until one of :split :stats :extents :scale-specs :grid :layout :finalized-scales :subplots build stops after this pipeline stage (inspect the intermediate data)
(plot/render-image iris-plot {:width 500 :height 300 :debug true})

:until stops the build after a stage; the built plot is data. Here the build stops after :extents - the extent of each mapped dimension:

(:extents (plot/build iris-plot {:until :extents}))
{:color
 {:extent
  {:kind :categorical, :values ["setosa" "versicolor" "virginica"]},
  :na? false},
 :y
 {:extent {:kind :numeric, :min 1.0, :max 6.9},
  :padding [0.05 0.05],
  :padding-px [0.0 0.0]},
 :x
 {:extent {:kind :numeric, :min 4.3, :max 7.9},
  :padding [0.05 0.05],
  :padding-px [0.0 0.0]}}

2.3 Colour vision

:cvd renders the plot as seen with a colour vision deficiency - a keyword, or [keyword severity] for the partial ones; plot/cvd-image converts any image. Red and green species, as rendered:

Option Type Default Description
:cvd a clojure2d color vision deficiency: a keyword (:protanopia, :deuteranopia, :tritanopia, :achromatopsia) or [keyword severity] (:protanomaly, :deuteranomaly, :tritanomaly, :achromatomaly; severity in [0 1]) or nil render as seen with a colour vision deficiency
(def red-green-plot (assoc iris-plot :scales {:color {:range [[215 48 39] [26 152 80] [69 117 180]]}}))
(plot/render-image red-green-plot {:width 500 :height 300})

… and as seen with deuteranopia - setosa and versicolor turn into similar olive tones, virginica stays blue:

(plot/render-image red-green-plot {:width 500 :height 300 :cvd :deuteranopia})

A partial deficiency takes a severity in [0 1] - here deuteranomaly at half strength:

(plot/render-image red-green-plot {:width 500 :height 300 :cvd [:deuteranomaly 0.5]})

2.4 Saving

plot/save writes an image (or the canvas of plot/render) to a file - the format from the extension (png, jpg, bmp, tiff …) - and returns it, so it ends a pipeline:

(-> iris-plot
    (plot/render-image {:width 800 :height 500})
    (plot/save "iris.png"))
source: notebooks/reference/render.clj