Last active
April 13, 2022 05:52
-
-
Save crosstyan/e5f9fc80974c089545dbb2b348d975f5 to your computer and use it in GitHub Desktop.
example using hy to plot some data. x is the domain and y is the number in that bin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x | y | |
---|---|---|
318 | 37 | |
319 | 58 | |
320 | 130 | |
321 | 142 | |
322 | 252 | |
323 | 458 | |
324 | 530 | |
325 | 700 | |
326 | 688 | |
327 | 770 | |
328 | 952 | |
329 | 810 | |
330 | 451 | |
331 | 372 | |
332 | 130 | |
333 | 32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; in 1.0a4 no need of bracket | |
(import numpy :as np | |
pandas :as pd | |
random | |
functools [reduce] | |
seaborn :as sns | |
matplotlib.pyplot :as plt | |
scipy.stats [norm]) | |
(setv data (pd.read_csv "25t.csv")) | |
(setv bin (- (get data.x 1) (get data.x 0))) | |
;; https://docs.hylang.org/en/alpha/tutorial.html?highlight=defn | |
;; 1.0a4 version remove &optional keyword. just pass a list | |
(defn gen-rnd-data [start [bin 1]] (+ start (* bin (random.random)))) | |
;; maybe I shouldn't listify it. Keep it lazy. | |
(defn gen-n [start n [bin 1]] (map (fn [x] (gen-rnd-data start bin)) (range n))) | |
;; (x, times) | |
(setv data-tuple (zip data.x data.y)) | |
(defn flat_map [f x] (reduce (fn [acc y] (+ acc (f y))) x [])) | |
(defn gen-fake-list [tuple [bin 1]] | |
(let [x (get tuple 0) | |
y (get tuple 1)] | |
(gen-n x y bin))) | |
;; have to listify gen-fake-list because flat_map implimentation is not lazy | |
(defn gen-concat-fake-list [tuple [bin 1]] (flat_map (fn [x] (list (gen-fake-list x bin))) tuple)) | |
(defn get-norm-dist-fit [xs] | |
(let [(, mu std) (norm.fit xs) | |
xmin (min xs) | |
xmax (max xs) | |
x-vals (np.linspace xmin xmax 1000) | |
y-vals (norm.pdf x-vals mu std)] | |
(, x-vals y-vals))) | |
(setv xs (gen-concat-fake-list data-tuple bin)) | |
;; https://engineersjourney.wordpress.com/2016/03/11/destructuring-in-hy/ | |
;; https://stackoverflow.com/questions/32899463/how-can-i-overlay-two-graphs-in-seaborn | |
(setv (, fig ax) (plt.subplots)) | |
;; (setv ax2 (ax.twinx)) | |
;; https://seaborn.pydata.org/examples/palette_choices.html?highlight=palette | |
(sns.set_theme :style "white" :context "talk") | |
;; (sns.distplot xs :fit norm :kde False) | |
(sns.histplot xs :ax ax :stat "density" :kde False :binwidth 1) | |
;; normal distribution | |
(setv (, x-vals y-vals) (get-norm-dist-fit xs)) | |
;; https://stackoverflow.com/questions/64130332/seaborn-futurewarning-pass-the-following-variables-as-keyword-args-x-y | |
(sns.lineplot :x x-vals :y y-vals) | |
(plt.xlabel "Numbers") | |
(plt.ylabel "Denisty") | |
(plt.show) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment