Skip to content

Instantly share code, notes, and snippets.

@realgenekim
Last active June 10, 2025 21:18
Show Gist options
  • Save realgenekim/5b527306eb7d80a557f4b7cec93d8db3 to your computer and use it in GitHub Desktop.
Save realgenekim/5b527306eb7d80a557f4b7cec93d8db3 to your computer and use it in GitHub Desktop.
create plotly graph from clojurecreate plotly from clojure, using libpython-clj
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
clj-python/libpython-clj {:mvn/version "2.026"}}
:aliases
{:dev {:jvm-opts ["--enable-native-access=ALL-UNNAMED"]}}}
(ns analysis.plotly-demo
"Simple Plotly visualization demo using libpython-clj2"
(:require [libpython-clj2.require :refer [require-python]]
[libpython-clj2.python :as py]))
;; Initialize Python
(py/initialize!)
;; Import Plotly
(require-python '[plotly.graph_objects :as go])
(defn simple-plot []
"The simplest possible Plotly plot"
(let [go (py/import-module "plotly.graph_objects")
Figure (py/get-attr go "Figure")
fig (Figure)]
;; Add a simple scatter plot
(py/py. fig add_trace
((py/get-attr go "Scatter") :x [1 2 3 4]
:y [10 15 13 17]))
;; Show it
(py/py. fig show)))
(defn bar-chart []
"Create a simple bar chart"
(let [go (py/import-module "plotly.graph_objects")
Figure (py/get-attr go "Figure")
Bar (py/get-attr go "Bar")
fig (Figure)]
(py/py. fig add_trace
(Bar :x ["A" "B" "C" "D"]
:y [1 3 2 4]))
(py/py. fig show)))
(defn plot-with-title []
"Create a plot with title and styling"
(let [go (py/import-module "plotly.graph_objects")
Figure (py/get-attr go "Figure")
Scatter (py/get-attr go "Scatter")
fig (Figure)]
;; Add data
(py/py. fig add_trace
(Scatter :x [1 2 3 4 5]
:y [2 4 3 5 6]
:mode "lines+markers"
:name "My Data"))
;; Update layout with title
(py/py. fig update_layout
:title "Simple Plotly Chart from Clojure!")
;; Show it
(py/py. fig show)))
(defn multi-trace-plot []
"Create a plot with multiple data series"
(let [go (py/import-module "plotly.graph_objects")
Figure (py/get-attr go "Figure")
Scatter (py/get-attr go "Scatter")
fig (Figure)]
;; Add first trace
(py/py. fig add_trace
(Scatter :x [1 2 3 4]
:y [10 15 13 17]
:mode "lines+markers"
:name "Series 1"))
;; Add second trace
(py/py. fig add_trace
(Scatter :x [1 2 3 4]
:y [16 18 17 19]
:mode "lines+markers"
:name "Series 2"))
;; Update layout
(py/py. fig update_layout
:title "Multiple Series Plot"
:xaxis_title "X Axis"
:yaxis_title "Y Axis")
;; Show it
(py/py. fig show)))
(defn save-to-html []
"Save a plot to HTML file"
(let [go (py/import-module "plotly.graph_objects")
Figure (py/get-attr go "Figure")
Scatter (py/get-attr go "Scatter")
fig (Figure)]
;; Create plot
(py/py. fig add_trace
(Scatter :x [1 2 3 4 5]
:y [1 4 9 16 25]
:name "Squares"))
;; Save to file
(py/py. fig write_html "my_plot.html")
(println "Plot saved to my_plot.html")
fig))
;; Example usage:
(comment
;; Run any of these:
(simple-plot)
(bar-chart)
(plot-with-title)
(multi-trace-plot)
(save-to-html)
;; Or create your own:
(let [go (py/import-module "plotly.graph_objects")
Figure (py/get-attr go "Figure")
fig (Figure)]
(py/py. fig add_trace
((py/get-attr go "Scatter") :x (range 10)
:y (map #(* % %) (range 10))))
(py/py. fig show)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment