Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
seaborn-data
(ns gigasquid.seaborn
(:require [libpython-clj.require :refer [require-python]]
[libpython-clj.python :as py :refer [py. py.. py.-]]
[gigasquid.plot :as plot]
[clojure.data.csv :as csv]))
(require-python '[seaborn :as sns])
(require-python '[matplotlib.pyplot :as pyplot])
(require-python '[pandas :as pandas])
;;; What is seaborn? Really cool statistical plotting
;;; sudo pip3 install seaborn
(sns/set) ;;; set default style
;;; code tutorial from https://seaborn.pydata.org/introduction.html
;;;; What is this thing that is loaded for the tidy data?
;;;; It is a dataframe
;;; Depending on each graphing function, it has the option to plot lists, numpy arrays, or DataFrames
;;; for the particular function `relplot` it requires a DataFrame
(def dots (sns/load_dataset "dots"))
(py. dots head)
;; align ... firing_rate
;; 0 dots ... 33.189967
;; 1 dots ... 31.691726
;; 2 dots ... 34.279840
;; 3 dots ... 32.631874
;; 4 dots ... 35.060487
;; [5 rows x 5 columns]
(py/python-type dots) ;=> :data-frame
(py/att-type-map dots)
(py.- dots dtypes)
;; align object
;; choice object
;; time int64
;; coherence float64
;; firing_rate float64
;; dtype: object
;;;;;;; Now let's load the same exact data from a csv file with Clojure Data Structures
;;; we will need to covert it over to a DataFrame to finally graph it
;;; Let's try to load it from csv
(def dots-csv (slurp "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/dots.csv"))
(def dots-data (doall (csv/read-csv dots-csv)))
(take 5 dots-data)
;; (["align" "choice" "time" "coherence" "firing_rate"]
;; ["dots" "T1" "-80" "0.0" "33.1899674031"]
;; ["dots" "T1" "-80" "3.2" "31.6917255152"]
;; ["dots" "T1" "-80" "6.4" "34.2798399644"]
;; ["dots" "T1" "-80" "12.8" "32.6318742985"])
;; also needs the number columns to acutally be numeric
(defn convert-numeric [row]
(mapv #(try (Float/parseFloat %)
(catch Exception e
%))
row))
(def num-dots-data (map convert-numeric dots-data))
(def dots-cols (first num-dots-data))
(def new-dots-data (rest num-dots-data))
dots-cols ;=> ["align" "choice" "time" "coherence" "firing_rate"]
(def dots-df (pandas/DataFrame new-dots-data :columns dots-cols))
(py/python-type dots-df) ;-> :data-frame
(py.- dots-df dtypes)
;; align object
;; choice object
;; time float64
;; coherence float64
;; firing_rate float64
;; dtype: object
(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-df))
;;;; lovely plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment