gnuplot gallery · examples/histograms.lms
Histograms
clustered, stacked and grouped histograms (gnuplot histograms.dem)
Loading the WebAssembly host…
What runs
The page fetches examples/histograms.bin, 2,515 bytes of lmcode bytecode
(sha256 4fd7463ef19d…), 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 histograms.dem: clustered, stacked and multi-group histograms (immigration.dat).
mkdir("images");
mkdir("build");
if (!file_exists("examples/data/immigration.dat")) {
mkdir("examples/data");
write_file("examples/data/immigration.dat", http_get("https://raw.githubusercontent.com/lmctl-ai/lmplot/main/examples/data/immigration.dat"));
}
p = _lmcode_ cli="lmplot" install="github:lmctl-ai/lmplot@v0.3.1";
plot.cmd("set datafile missing '-'");
plot.cmd("set key noenhanced"); # keep underscores in names like United_Kingdom literal
plot.script("set style data histogram\nset style fill solid border -1\nset boxwidth 0.9");
# Image 1: clustered histogram of four columns.
plot.png("images/histograms-1.png", 800, 600);
plot.script("set title \"US immigration from Northern Europe\\nPlot selected data columns as histogram of clustered boxes\"\nset yrange [0:300000]\nset style histogram cluster gap 1\nset xtic rotate by -45 scale 0");
plot.cmd("plot 'examples/data/immigration.dat' using 6:xtic(1) ti col, '' u 12 ti col, '' u 13 ti col, '' u 14 ti col");
plot.close();
# Image 2: all countries as a row-stacked histogram.
plot.png("images/histograms-2.png", 900, 600);
plot.script("set title \"US immigration from Europe by decade\\nPlot as stacked histogram\"\nset key invert reverse Left outside\nset key autotitle columnheader\nset yrange [0:7e6]\nset style histogram rowstacked\nset boxwidth 0.75");
plot.cmd("plot 'examples/data/immigration.dat' using 2:xtic(1), for [i=3:22] '' using i");
plot.close();
# Image 3: several histogram groups with newhistogram.
plot.png("images/histograms-3.png", 800, 600);
plot.script("reset\nset key noenhanced\nset datafile missing '-'\nset style data histogram\nset style fill solid noborder\nset boxwidth 0.95");
plot.script("set title \"Immigration from different regions\\n(give each histogram a separate title)\"\nset key under nobox\nset style histogram clustered gap 1 title offset 2,0.25");
plot.script("set xtics nomirror rotate by -45 scale 0 font ',8'\nset ytics font ',8'\nset grid y");
plot.cmd("set xlabel '(note: histogram titles have specified offset relative to X-axis label)' offset 0,-2");
plot.cmd("plot newhistogram 'Northern Europe', 'examples/data/immigration.dat' using 'Sweden':xtic(1) t col, '' u 'Denmark' t col, '' u 'Norway' t col, newhistogram 'Southern Europe', '' u 'Greece':xtic(1) t col, '' u 'Romania' t col, '' u 'Yugoslavia' t col, newhistogram 'British Isles', '' u 'Ireland':xtic(1) t col, '' u 'United_Kingdom' t col");
plot.close();
end _lmcode_
r = run_lmcode(p);
assert(r.ok, json_stringify(r));
cp = system cp images/histograms-1.png images/histograms.png;
assert(cp.code == 0);
println("wrote images/histograms.png (and images/histograms-1.png .. images/histograms-3.png)");