Skip to content

Commit

Permalink
python plotting - impl draft
Browse files Browse the repository at this point in the history
  • Loading branch information
daslu committed Jul 23, 2024
1 parent 6427a6c commit c194d25
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions projects/datavis/python/plotting/notebooks/index.clj
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
(ns index
(:require [libpython-clj2.require :refer [require-python]]
[libpython-clj2.python :refer [py. py.. py.-] :as py]))


;; inspiration: http://gigasquidsoftware.com/blog/2020/01/18/parens-for-pyplot/

(require-python 'matplotlib.pyplot
'matplotlib.backends.backend_agg
'numpy)

(defmacro with-pyplot
"Takes forms with mathplotlib.pyplot and returns a showable (SVG) plot."
[& body]
`(let [_# (matplotlib.pyplot/clf)
fig# (matplotlib.pyplot/figure)
agg-canvas# (matplotlib.backends.backend_agg/FigureCanvasAgg fig#)
path# (:path (tempfiles/tempfile! ".svg"))]
~(cons 'do body)
(py. agg-canvas# "draw")
(matplotlib.pyplot/savefig path#)
(-> path#
slurp
kind/html)))


(defn pyplot
"Takes a function plotting using mathplotlib.pyplot, and returns a showable (SVG) plot"
[plotting-function]
(let [_ (matplotlib.pyplot/clf)
fig (matplotlib.pyplot/figure)
agg-canvas (matplotlib.backends.backend_agg/FigureCanvasAgg fig)
path (:path (tempfiles/tempfile! ".svg"))]
(plotting-function)
(py. agg-canvas "draw")
(matplotlib.pyplot/savefig path)
(-> path
slurp
kind/html)))

0 comments on commit c194d25

Please sign in to comment.