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"}}}Install the python dependencies
pip3 install seabornpip3 install matplotlibAnd 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.-]]))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"))))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])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)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))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))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))And show data analysis between categorical values
(plot/with-show (sns/catplot :x "day" :y "total_bill" :hue "smoker" :kind "swarm" :data tips))(plot/with-show (sns/catplot :x "day" :y "total_bill" :hue "smoker" :kind "bar" :data tips))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))(plot/with-show (sns/pairplot :data iris :hue "species"))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