Parens for Python - Seaborn Visualizations

We are going to explore some more Python libraries through the use of libpython-clj.

This time, we are going to focus on a statistical visualization library called Seaborn.

{:deps
 {org.clojure/clojure {:mvn/version "1.10.1"}
  cnuernber/libpython-clj {:mvn/version "1.36"}}}
deps.edn
Clojure

Install the python dependencies

pip3 install seaborn
pip3 install matplotlib
18.7s
Clj & Python env (Bash in Clojure)

And we're all set, and can experiment with the examples.

Seaborn

(ns gigasquid.plot
  (:require [libpython-clj.require :refer [require-python]]
            [libpython-clj.python :as py :refer [py. py.. py.-]]))
15.7s
Clj & Python env (Clojure)

First, we have to define a quick macro to show the plotting for our local system. This allows matplotlib, (the library that seaborn is built on), to be able to be shown headlessly.

;;;; have to set the headless mode before requiring pyplot
(def mplt (py/import-module "matplotlib"))
(py. mplt "use" "Agg")
(require-python 'matplotlib.pyplot)
(require-python 'matplotlib.backends.backend_agg)
(defmacro with-show
  "Takes forms with mathplotlib.pyplot to then show locally"
  [& body]
  `(let [_# (matplotlib.pyplot/clf)
         fig# (matplotlib.pyplot/figure)
         agg-canvas# (matplotlib.backends.backend_agg/FigureCanvasAgg fig#)]
     ~(cons 'do body)
     (py. agg-canvas# "draw")
     (matplotlib.pyplot/savefig (str "results/" gensym ".png"))))
2.6s
Clj & Python env (Clojure)
gigasquid.plot/with-show

Seaborn is a really cool statistical plotting library based on matplotlib. First, we need to require the python libs through interop.

(ns gigasquid.seaborn
  (:require [libpython-clj.require :refer [require-python]]
            [libpython-clj.python :as py :refer [py. py.. py.-]]
            [gigasquid.plot :as plot]))
(require-python '[seaborn :as sns])
(require-python '[matplotlib.pyplot :as pyplot])
1.0s
Clj & Python env (Clojure)
:ok

Next we are going to follow along the code tutorial from https://seaborn.pydata.org/introduction.html

(seaborn/set)  ;;; set default style
(def dots (sns/load_dataset "dots"))
(take 5 dots)
0.3s
Clj & Python env (Clojure)
List(5) ("align", "choice", "time", "coherence", "firing_rate")

We can show the statistical relationship by plotting.

(plot/with-show
  (sns/relplot :x "time" :y "firing_rate" :col "align"
               :hue "choice" :size "coherence" :style "choice"
               :facet_kws {:sharex false} :kind "line"
               :legend "full" :data dots))
2.0s
Clj & Python env (Clojure)

Here are some statistical estimation and error bars.

(def fmri (sns/load_dataset "fmri"))
(plot/with-show
  (sns/relplot :x "timepoint" :y "signal" :col "region"
               :hue "event" :style "event" :kind "line"
               :data fmri))
4.3s
Clj & Python env (Clojure)

We can enhance a scatter plot to include a linear regression model

(def tips (sns/load_dataset "tips"))
(plot/with-show
  (sns/lmplot :x "total_bill" :y "tip" :col "time" :hue "smoker" :data tips))
2.1s
Clj & Python env (Clojure)

And show data analysis between categorical values

(plot/with-show
  (sns/catplot :x "day" :y "total_bill" :hue "smoker" :kind "swarm" :data tips))
0.7s
Clj & Python env (Clojure)
(plot/with-show
  (sns/catplot :x "day" :y "total_bill" :hue "smoker" :kind "bar" :data tips))
0.9s
Clj & Python env (Clojure)

Finally we can also visualize dataset structures.

(def iris (sns/load_dataset "iris"))
(plot/with-show
  (sns/jointplot :x "sepal_length" :y "petal_length" :data iris))
0.9s
Clj & Python env (Clojure)
(plot/with-show
  (sns/pairplot :data iris :hue "species"))
6.0s
Clj & Python env (Clojure)

I hope you've enjoyed our quick tour of Seaborn. You can check out all the code and other examples here https://github.com/gigasquid/libpython-clj-examples/tree/master/src/gigasquid

Runtimes (1)