Skip to content

Commit c194d25

Browse files
committed
python plotting - impl draft
1 parent 6427a6c commit c194d25

File tree

1 file changed

+36
-0
lines changed
  • projects/datavis/python/plotting/notebooks

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
11
(ns index
22
(:require [libpython-clj2.require :refer [require-python]]
33
[libpython-clj2.python :refer [py. py.. py.-] :as py]))
4+
5+
6+
;; inspiration: http://gigasquidsoftware.com/blog/2020/01/18/parens-for-pyplot/
7+
8+
(require-python 'matplotlib.pyplot
9+
'matplotlib.backends.backend_agg
10+
'numpy)
11+
12+
(defmacro with-pyplot
13+
"Takes forms with mathplotlib.pyplot and returns a showable (SVG) plot."
14+
[& body]
15+
`(let [_# (matplotlib.pyplot/clf)
16+
fig# (matplotlib.pyplot/figure)
17+
agg-canvas# (matplotlib.backends.backend_agg/FigureCanvasAgg fig#)
18+
path# (:path (tempfiles/tempfile! ".svg"))]
19+
~(cons 'do body)
20+
(py. agg-canvas# "draw")
21+
(matplotlib.pyplot/savefig path#)
22+
(-> path#
23+
slurp
24+
kind/html)))
25+
26+
27+
(defn pyplot
28+
"Takes a function plotting using mathplotlib.pyplot, and returns a showable (SVG) plot"
29+
[plotting-function]
30+
(let [_ (matplotlib.pyplot/clf)
31+
fig (matplotlib.pyplot/figure)
32+
agg-canvas (matplotlib.backends.backend_agg/FigureCanvasAgg fig)
33+
path (:path (tempfiles/tempfile! ".svg"))]
34+
(plotting-function)
35+
(py. agg-canvas "draw")
36+
(matplotlib.pyplot/savefig path)
37+
(-> path
38+
slurp
39+
kind/html)))

0 commit comments

Comments
 (0)