gnuplot gallery · examples/boxplot.lms
Box plots
boxplots, factor colouring (gnuplot boxplot.dem)
Loading the WebAssembly host…
What runs
The page fetches examples/boxplot.bin, 1,524 bytes of lmcode bytecode
(sha256 43ae2a88f6cd…), and hands it to the same lmplot.wasm every example uses.
The bytecode calls the host's Plot functions (plot.cmd, plot.data, plot.num,
plot.png …); gnuplot draws into an in-memory file system and the page shows the files the program wrote.
In SVG mode plot.png("x.png", w, h) writes x.svg with gnuplot's svg terminal; PNG mode uses lmplot's
own rasterizer and fonts, the same bytes as the native binary.
The program was compiled ahead of time from the source on the right with lmscript; the lmscript lines around the
_lmcode_ block (data generation, ${...} values, run_lmcode) ran at compile time.
# Demo boxplot.dem: box plots of silver.dat and of energy usage grouped by factor.
mkdir("images");
mkdir("build");
if (!file_exists("examples/data/silver.dat")) {
mkdir("examples/data");
write_file("examples/data/silver.dat", http_get("https://raw.githubusercontent.com/lmctl-ai/lmplot/main/examples/data/silver.dat"));
}
if (!file_exists("examples/data/energy_circles.dat")) {
mkdir("examples/data");
write_file("examples/data/energy_circles.dat", http_get("https://raw.githubusercontent.com/lmctl-ai/lmplot/main/examples/data/energy_circles.dat"));
}
p = _lmcode_ cli="lmplot" install="github:lmctl-ai/lmplot@v0.3.1";
# Image 1: two boxplots with custom x tics.
plot.png("images/boxplot-1.png", 800, 600);
plot.script("set style fill solid 0.5 border -1\nset style boxplot outliers pointtype 7\nset style data boxplot\nset boxwidth 0.5\nset pointsize 0.5\nunset key\nset border 2");
plot.script("set xtics ('A' 1, 'B' 2) scale 0.0\nset xtics nomirror\nset ytics nomirror\nset yrange [0:100]");
plot.cmd("plot 'examples/data/silver.dat' using (1):2, '' using (2):(5*$3)");
plot.close();
# Image 2: boxplot split by factor (column 4), colours from linetypes.
plot.png("images/boxplot-2.png", 900, 600);
plot.script("set xtics auto\nset yrange [*:*]\nset ylabel 'Billion Tons of Oil Equivalent'\nset datafile columnheaders");
plot.script("set linetype 51 lc 'dark-red'\nset linetype 52 lc 'dark-red'\nset linetype 53 lc 'dark-red'\nset linetype 54 lc 'midnight-blue'\nset linetype 55 lc 'sea-green'\nset linetype 56 lc 'sea-green'\nset linetype 57 lc 'sea-green'");
plot.cmd("set title \"Distribution of energy usage of the continents, grouped by type of energy source\\n\"");
plot.cmd("plot 'examples/data/energy_circles.dat' using (1):($8/1.e6):(0):4 lt 51 lc variable");
plot.close();
end _lmcode_
r = run_lmcode(p);
assert(r.ok, json_stringify(r));
cp = system cp images/boxplot-2.png images/boxplot.png;
assert(cp.code == 0);
println("wrote images/boxplot.png (and images/boxplot-1.png .. images/boxplot-2.png)");