- はじめに -
Rustでグラフを描画したいと思った時に調べたクレートとその実装、機能のまとめた時のメモ。
現状はplottersを使っておけば間違いなさそうだが、目的によっては機能で選択する場合もありそう。
- 前提知識 -
グラフの描画までの機能としては、matplotlibのようにaxisやviewを構造体として持っているライブラリもあれば、受け取った配列をそのままgnuplotのスクリプトに変換するライブラリもある。
詳細は後述するが、当然この構造に依存してインターフェースが変わったり、出来ること出来ないことがある。
plotを想定したグラフデータの出力方法は大きく3つに分かれる。
SVG等を通して画像ファイルとして出力する方法、jsやwasmやhtmlテンプレートエンジンを利用してHTMLベースで出力する方法、テキストベース(アスキーアート)として表示する方法である。
また、Jupyter NotebookのRust Kernelとして現状開発が継続しているものにevcxrというライブラリがあり、こちらに出力する事が出来るかも差別化の点に入る。
github.com
OpenCV等の画像処理系ライブラリを用いてもグラフの描画はもちろん行えるが、今回はグラフ描画を軸としたライブラリの調査であり対象とはしない。
- グラフ描画クレートざっくりまとめ -
2021/09/21時点での大まかな実装とライブラリをまとめる
plotters
A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely 🦀 📈🚀
latest commit: 2021/09/17, star: 1.5K
github.com
- 最も開発が活発である
- 一般的なplotに加えて複数plot、3次元plot、Animationをサポート
- matplotlibに似た思想のAPIを持つ
- matplotlibにおけるfigure、axesとDrawingArea、ChartContextの対応と考えて良さそう
- title等の持たれ方が若干異なるがptl.figureが柔軟すぎると考えれば良さそう
- matplotlibにおけるfigure、axesとDrawingArea、ChartContextの対応と考えて良さそう
- 描画先 (backend)
- 画像描画のクレートをbackendと呼びそれらとの間にcoordinator層を挟んでいる
- plotters内における論理座標系とバックエンド座標系の変換を行う
- https://github.com/38/plotters/tree/master/src/coord
- Bitmap BackendとSVG Backendがデフォルトで入る
- coordから受け取った座標をgifクレート/svgクレート用の画像フォーマットに変換する機構が別のクレートで書かれている
- https://github.com/plotters-rs/plotters-svg
- https://github.com/plotters-rs/plotters-bitmap
- HTML5 Canvas Backend
- ブラウザのAPIを叩くwasm-bindgenのweb_sysを利用してcanvasを操作するwasmを生成する
- https://github.com/plotters-rs/plotters-canvas
- GTK/Cairo Backend
- cairoのrustバインディングであるgtk-rsを叩く形. GNOME上で使えそう
- https://github.com/plotters-rs/plotters-cairo
- piston、text Backened
- 一応pistonやtextのbackendのベースが存在(開発は進んでいなさそうなので必要なら自分で)
- https://github.com/PistonDevelopers/piston_window
- https://github.com/plotters-rs/plotters-text
- 画像描画のクレートをbackendと呼びそれらとの間にcoordinator層を挟んでいる
- evcxr_figureメソッドがありjupyter notebook上に描画できる
- SVGフォーマットをEVCXR_CONTENTでwrapする簡易なもの
- 他のクレートでも同等の実装で簡単にevcxr出力できそう
- https://github.com/38/plotters/blob/96d51844ea2febc36dae5f07901db54f10b90c08/src/evcxr.rs
- plottersのみplotters-rsプロジェクト配下にない(今後移行されていくかも?)
- チュートリアルに空の部分が多くメンテナになるチャンス
以下参考に成り得る文献
- RustのドローイングライブラリPlottersの紹介 | MoT Lab (Mobility Technologies Engineering Blog) https://lab.mo-t.com/blog/rust-plotters
- RustのPlottersでグラフ描画を試す - Qiita https://qiita.com/showchan33/items/0b58b598c5e0e7bf1689
- petgraph_review https://timothy.hobbs.cz/rust-play/petgraph_review.html
- evcxr_jupyterによるRust on Notebook | lab note https://lab.ar90n.net/rust/jupyter/2020/11/15/rust-on-nodebook-with-evcxr-jupyter.html
- evcxr-jupyter-integration https://plotters-rs.github.io/plotters-doc-data/evcxr-jupyter-integration.html
plotly
Plotly for Rust
latest commit: 2021/07/15, star: 467
github.com
- d3.jsとstack.glを利用しグラフを描画する著名なplotly.jsのwrapper
- plotly公式ではない
- ndarrayから直接plotする機能がある
- plotly-xxxxx.min.jsをクレート内に含みrust実装のテンプレートエンジンであるaskamaを経由して画像をレンダリングしている
- 画像ファイルへの変換においてはkaleidoというプロジェクトをwrapしている
- png, jpeg, webp, svg, pdf, epsに変換する
- https://github.com/plotly/Kaleido
- https://github.com/igiagkiozis/plotly/tree/master/plotly_kaleido
- notebook_display, lab_displayなるメソッドでjupyter notebookに描画できる
- テンプレートエンジンで生成したHTMLをそのままEVCXR_CONTENTに変換している
- https://github.com/igiagkiozis/plotly/blob/master/plotly/src/plot.rs#L282
- チュートリアルが存在する
plotlib
Data plotting library for Rust
latest commit: 2021/02/01, star: 335
github.com
- 非常にmatplotlibを意識したであろう実装になっている
- 開発は滞り気味
- Vecやndarrayに対応
- 自前でaxisやviewの構造体を持っている
- textでの描画、svgクレートを使った画像での描画に対応
- ドキュメントにはpng対応と書いてあるが未開発っぽい
- https://github.com/milliams/plotlib/issues/39
- matplotlibに似た思想のAPIを持つ
- matplotlibにおけるfigure、axesがview, plotに当たる
以下参考に成り得る文献
- Rustでグラフをプロットする - Qiita https://qiita.com/termoshtt/items/e07f2f1714fccd750b55
- グラフをプロットする(plotlib crate)|Rustで数値計算 https://zenn.dev/termoshtt/books/b4bce1b9ea5e6853cb07/viewer/plotlib
poloto
A simple 2D plotting library that outputs graphs to SVG that can be styled using CSS.
latest commit: 2021/09/17, star: 28
github.com
- SVGをCSSでイジれる事をウリにしたクレート
- 2021年1月から開発
- 描画が綺麗
- taggerなるhtmlやSVGを生成できるテンプレートエンジンを使っている
- https://github.com/tiby312/tagger
- 対応するHTMLにCSSを流してrenderingしてSVGに変換している
- APIとして対応はしていないがstdoutにEVCXR_CONTENTとしてレンダリングしたものを流せばevcxrでも表示できる
- Broad-Phase Collision Detectionを行うbroccoliなるクレートのドキュメントで使われている
- CSS適応が簡単で描画が綺麗だが覚えるまでが割と大変そう
以下参考に成り得る文献
- Rust for Data Science: Tutorial 1 - DEV Community https://dev.to/davidedelpapa/rust-for-data-science-tutorial-1-4g5j
rustplotlib
A pure Rust visualization library inspired by D3.js
latest commit: 2021/07/13, star: 1116
github.com
- D3.jsをまるっとrustで書き直している
- plotly.jsのベースがD3.jsなので思想はかなりplotlyに近い
- https://plotly.com/javascript/
- 描画がかなり綺麗な印象
- 最後はsvgクレートでSVGに書き出している (.to_svg)
- 発想としてはかなり壮大なプロジェクトだが、更新はしばらく止まっていそう
- multiviewなどに未対応だが今後開発されるかは実装を見る限り微妙そう
- 対応するplot形式を沢山作る方針っぽい
RustGnuplot
A Rust library for drawing plots, powered by Gnuplot
latest commit: 2021/09/01, star: 324
github.com
以下参考に成り得る文献
- [Rust] 練習で正規分布描いてみた [gnuplot] - MATHGRAM https://www.mathgram.xyz/entry/rust/gd
- Rustで機械学習(rusty-machine, RustGnuplot) - TSUGULOG https://yoshitsugu.net/posts/2017-11-14-beginning-machine-learning-with-rust.html
preexplorer
Externalize easily the plotting process from Rust to gnuplot.
latest commit: 2021/09/06, star: 4
github.com
- gnuplotのwrapper
- やっている事はRustGnuplotと同じ
- gnuplotコマンド用のスクリプトを生成して叩く
- https://github.com/saona-raimundo/preexplorer/blob/master/src/traits.rs#L865
- なのでgnuplotコマンドのinstallが別途必要
vega_lite_4.rs
rust api for vega-lite v4
latest commit: 2021/01/22, star: 7
github.com
- Pythonで言う所のAltair
- Vega Lite(vega-lite.js)にJsonAPIがあるのでそれを叩くための実装を用意したもの
- https://vega.github.io/vega-lite/
- vega-lite.jsが入ったHTMLをテンプレートエンジンでビルドしている
- https://github.com/procyon-rs/vega_lite_4.rs/blob/master/src/show_vega.rs
- nalgebraやndarray、rulinalg等の主要な行列ライブラリに対応している
- showtaを作っている人と同じ
- https://github.com/procyon-rs/showata
- HTMLを生成するためのツール
- jupyter notebook上に描画する事を目的としている
- tableと画像をHTMLに変換するためのツール
- showtaを経由してevcxrで表示できる
- https://github.com/procyon-rs/showata
- version4に対応したもので、vega_lite_3.rsも存在する
dataplotlib
Scientific plotting library for Rust
latest commit: 2017/10/14, star: 57
github.com
chord_rs
Rust crate for creating beautiful interactive Chord Diagrams.
latest commit: 2021/01/07, star: 22
github.com
- Chord Diagramsを描画するためだけのクレート
- Chord PROなるAPIを叩くclientであり、描画機構については分からない
- アスキーアート系のクレート -
plotlib等でも対応しているが、CLIなどで扱えるようにtext形式でplotするクレートがいくつかある。以下簡単に。
- loony-bean/textplots-rs: Terminal plotting library for Rust https://github.com/loony-bean/textplots-rs
- orhanbalci/rasciigraph: Zero dependency Rust crate to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies. https://github.com/orhanbalci/rasciigraph
- jakobhellermann/piechart: a rust crate for drawing fancy pie charts in the terminal https://github.com/jakobhellermann/piechart
- milliams/plot: Command-line plotting tool written in Rust https://github.com/milliams/plot
plotという強気の命名がここにある
- 記事外で参考になりそうな記事 -
- グラフィック系のライブラリのまとめ
- Wiki - AGuideToRustGraphicsLibraries2019 https://wiki.alopex.li/AGuideToRustGraphicsLibraries2019
- web監視ライブラリのlorikeetのダッシュボードライブラリを作るcetra3氏のブログ
- https://github.com/cetra3/lorikeet
- htmlレンダリングによるグラフplotのノウハウ
- Drawing SVG Graphs with Rust https://cetra3.github.io/blog/drawing-svg-graphs-rust/
- Rustでデータ分析をしようという記事と書籍
- PlottersとPlotly、ndarrayの取り扱い等
- https://datacrayon.com/posts/programming/rust-notebooks/preface/
- おわりに -
まとめた。
Rustでデータ分析する所までやるユーザはあまりいなさそうで、定常分析や監視に使うならHTMLレンダリングは筋が良さそう。
一方plottersが一番活発に開発されているので、何を選択しましょうかという感じ。