gnuplot gallery · examples/layout.lms
Multiplot layouts
multiplot layouts and stacked panels (gnuplot layout.dem)
Loading the WebAssembly host…
What runs
The page fetches examples/layout.bin, 3,535 bytes of lmcode bytecode
(sha256 809fea59135f…), 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.
# Multiplot layout: gnuplot "layout.dem" (auto-layout of several plots on one page).
# Image 1: layout 3,1 (rows). Image 2: layout 1,3 with rotated tics.
# Image 3: stacked plots sharing one x axis (zero top/bottom margins).
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"));
}
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"));
}
p = _lmcode_ cli="lmplot" install="github:lmctl-ai/lmplot@v0.3.1";
# The histogram/line data files live in examples/data (paths relative to repo root).
let hist = "plot 'examples/data/immigration.dat' using 2 with histograms, '' using 7 with histograms, '' using 8 with histograms, '' using 11 with histograms";
# --- 1: three plots stacked vertically via "multiplot layout 3,1" ---
plot.png("images/layout-1.png", 800, 800);
plot.cmd("set multiplot layout 3, 1 title 'Multiplot layout 3, 1' font ',14'");
plot.cmd("set tmargin 3");
plot.cmd("unset key");
plot.cmd("set title 'Plot 1'");
plot.cmd("plot sin(x)/x");
plot.cmd("set title 'Plot 2'");
plot.cmd("plot 'examples/data/silver.dat' using 1:2 ti 'silver.dat'");
plot.cmd("set style histogram columns");
plot.cmd("set style fill solid");
plot.cmd("set key autotitle column");
plot.cmd("set boxwidth 0.8");
plot.cmd("set format y ' '");
plot.cmd("set tics scale 0");
plot.cmd("set title 'Plot 3'");
plot.cmd(hist);
plot.cmd("unset multiplot");
plot.close();
# --- 2: the same three plots side by side, layout 1,3 ---
plot.png("images/layout-2.png", 1000, 450);
plot.cmd("reset");
plot.cmd("set multiplot layout 1,3 title 'Multiplot layout 1, 3' font ',14'");
plot.cmd("set xtics rotate");
plot.cmd("set bmargin 5");
plot.cmd("unset key");
plot.cmd("set title 'Plot 1'");
plot.cmd("plot sin(x)/x");
plot.cmd("set title 'Plot 2'");
plot.cmd("plot 'examples/data/silver.dat' using 1:2 ti 'silver.dat'");
plot.cmd("set title 'Plot 3'");
plot.cmd("set style histogram columns");
plot.cmd("set style fill solid");
plot.cmd("set key autotitle column");
plot.cmd("set boxwidth 0.8");
plot.cmd("set format y ' '");
plot.cmd("set tics scale 0");
plot.cmd(hist);
plot.cmd("unset multiplot");
plot.close();
# --- 3: stacked plots, zero vertical margins; the 4th slot holds the axis labels ---
plot.png("images/layout-3.png", 800, 600);
plot.cmd("reset");
plot.cmd("set tmargin 0");
plot.cmd("set bmargin 0");
plot.cmd("set lmargin 10");
plot.cmd("set rmargin 3");
plot.cmd("unset xtics");
plot.cmd("unset ytics");
plot.cmd("set multiplot layout 4,1 title 'Auto-layout of stacked plots\n' font ',12'");
plot.cmd("set key autotitle column nobox samplen 1 noenhanced");
plot.cmd("unset title");
plot.cmd("set style data boxes");
plot.cmd("set yrange [0 : 800000]");
plot.cmd("plot 'examples/data/immigration.dat' using 3 lt 1");
plot.cmd("plot 'examples/data/immigration.dat' using 8 lt 3");
plot.cmd("set xtics nomirror");
plot.cmd("set tics scale 0 font ',8'");
plot.cmd("set xlabel 'Immigration to U.S. by Decade'");
plot.cmd("plot 'examples/data/immigration.dat' using 21:xtic(1) lt 4");
plot.cmd("unset multiplot");
plot.close();
end _lmcode_
r = run_lmcode(p);
assert(r.ok, json_stringify(r));
cp = system cp images/layout-1.png images/layout.png;
assert(cp.code == 0);
println("wrote images/layout.png (and images/layout-1.png .. images/layout-3.png)");