Oz 项目使用教程
1. 项目介绍
Oz 是一个基于 Clojure 和 ClojureScript 的数据可视化和科学文档处理库,它使用 Vega 和 Vega-Lite 作为核心可视化工具。Vega 和 Vega-Lite 是用于描述交互式数据可视化的声明性语法,它们基于 Grammar of Graphics,类似于 R 语言中的 ggplot2 库。Oz 提供了丰富的 API,支持在 Clojure 和 ClojureScript 中创建和展示数据可视化,并且可以嵌入到 Jupyter 笔记本中。
2. 项目快速启动
2.1 安装依赖
首先,确保你已经安装了 Leiningen,然后通过以下命令将 Oz 添加到你的项目依赖中:
(defproject my-project "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.10.3"]
[metasoarous/oz "1.6.0-alpha36"]])
2.2 启动 Oz 服务器
在你的 Clojure 项目中,启动 Oz 服务器以便在浏览器中查看可视化结果:
(require '[oz.core :as oz])
(oz/start-server)
2.3 创建第一个可视化
以下是一个简单的 Vega-Lite 线图示例:
(defn play-data [& names]
(for [n names i (range 20)]
{:time i :item n :quantity (+ (Math/pow (* i (count n)) 0.8) (rand-int (count n)))}))
(def line-plot
{:data {:values (play-data "monkey" "slipper" "broom")}
:encoding {:x {:field "time" :type "quantitative"}
:y {:field "quantity" :type "quantitative"}
:color {:field "item" :type "nominal"}}
:mark "line"})
(oz/view line-plot)
运行上述代码后,你应该会在浏览器中看到一个简单的线图。
3. 应用案例和最佳实践
3.1 嵌入到 Jupyter 笔记本
Oz 支持将可视化嵌入到 Jupyter 笔记本中。首先,确保你已经安装了 Clojupyter 或 IClojure 内核,然后使用以下代码:
(require '[oz.notebook :as oznb])
(oznb/view line-plot)
3.2 创建复杂的可视化
你可以通过组合多个 Vega-Lite 和 Vega 图表来创建复杂的可视化。例如,以下代码展示了如何在一个页面中嵌入多个图表:
(def viz
[:div
[:h1 "Look ye and behold"]
[:p "A couple of small charts"]
[:div {:style {:display "flex" :flex-direction "row"}}
[:vega-lite line-plot]
[:vega-lite stacked-bar]]
[:p "A wider, more expansive chart"]
[:vega contour-plot]
[:h2 "If ever, oh ever a viz there was, the vizard of oz is one because, because, because..."]
[:p "Because of the wonderful things it does"]])
(oz/view viz)
4. 典型生态项目
4.1 Vega Editor
Vega Editor 是一个强大的编辑工具,用于编辑和共享 Vega 和 Vega-Lite 数据可视化。你可以通过它来创建和调试可视化,然后将代码集成到 Oz 中。
4.2 Ozviz
Ozviz 是 Oz 的一个姐妹项目,类似于 Vega Editor,但它专注于共享和编辑包含 Vega 和 Vega-Lite 可视化的 Hiccup 文档。
4.3 Voyager
Voyager 是一个类似于 Tableau 的工具,用于探索数据并构建可导出的 Vega 和 Vega-Lite 可视化。它可以帮助你快速生成复杂的可视化,然后将代码集成到 Oz 中。
通过这些工具和 Oz 的结合,你可以创建出功能强大且美观的数据可视化。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考