The Holy Java

Building the right thing, building it right, fast

Clojure – comparison of gnuplot, Incanter, oz/vega-lite for plotting usage data

Posted by Jakub Holý on November 4, 2018

What is the best way to plot memory and CPU usage data (mainly) in Clojure? I will compare gnuplot, Incanter with JFreeChart, and vega-lite (via Oz). (Spoiler: I like Oz/vega-lite most but still use Incanter to prepare the data.)

The data looks like this:

;; sec.ns | memory | CPU %
1541052937.882172509 59m 0.0
1541052981.122419892 78m 58.0
1541052981.625876498 199m 85.9
1541053011.489811184 1.2g 101.8

The data has been produced by monitor-usage.sh.

The tools

Gnuplot 5

Gnuplot is the simplest, with a lot available out of the box. But it is also somewhat archaic and little flexible.

Here is the code:

And here is an example output:

Pros: Feature-rich (support bytes axis, two Y axes, reading data).

Cons: Archaic language, less flexible; I couldn’t stop it from drawing X labels over each other when I had data for a longer period.

Incanter (JFreeChart)

Incanter is (was?) the go to tool for data analysis in Clojure and it includes the Java library JFreeChart for charting. There has been some development lately (integrating clojure.core.matrix) but otherwise it is quite stagnating. That’s why there have been newer community efforts.

Here is the first part of the code, that prepares the data for charting (also used for Oz):

And here is the code to format the chart (the most difficult part was to display kB / MB / GB values on the axis in a nice way; I really missed Gnuplot’s out-of-the-box support here):

That is quite insane amount of work, huh? And the result:

vega-lite via Oz

Vega and Vega-lite visualization grammars: Vega is a declarative format for creating, saving, and sharing visualization designs. With Vega, visualizations are described in JSON, and generate interactive views using either HTML5 Canvas or SVG.

Vega-lite (and Vega) is the new kid on the block. It has learned a lot from D3 and R’s ggplot2 and is intended for data exploration, interactive charts, and making it possible to combine multiple charts on the same page – while making this as simple as possible. It is based on the Grammar of Graphics so it is presumabely well thought-through.

Oz is a thin wrapper around vega/vega-lite that makes it possible to interact with the browser from Clojure REPL, with Clojure data.

(Note: Oz currently uses VL v2.6 while the latest VL is v3rc8, which fixes some limitations in v2.)

As mentioned above, we use the same usage-chart-preparation.clj as the pure-Incanter example to prepare the data for charting. Then, to plot them:

It is more work than in Gnuplot but it provides more value and is much more powerful.

Here is the output (generated with VL v3 so the labels have the same color as the lines):

The awesome thing is that the chart and data can be encoded in an URL so that you can open in in the online Vega Editor and play with it.

Pros: Good and powerful design, modern, interactive, promising.

Cons: The community is very small so it is harder to get help, it isn’t so mature yet (e.g. it was impossible to add a legend to my multi-layer chart). You have to transform your data into JSON so it likely isn’t suitable for huge amounts of it.

Other options

Clojurewerkz/envision is “a small, easy to use Clojure library for data processing, cleanup and visualisation. [..] Main idea of this library is to make exploratory analysis more interactive and visual, although in programmer’s way.” ClojureWerkz is known for its commitment to project quality and maintenance so that is good, on the other hand the last code change has been 2 years ago.

Outside of Clojure, I would expect Python to have some very good charting libraries.

Conclusion

Nothing is optimal 🙂 but vega-lite is very promising, I will continue to use it – and I will also still use Incanter to process and prepare the data.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: