gnuplot gallery · examples/heatmaps.lms

Heat maps

images from matrix and XYZ data (gnuplot heatmaps.dem)

Loading the WebAssembly host…

What runs

The page fetches examples/heatmaps.bin, 1,790 bytes of lmcode bytecode (sha256 75d98358ab12…), 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.

# heatmaps.dem: 2D heat maps from matrix and XYZ data.
mkdir("images");
mkdir("build");
p = _lmcode_ cli="lmplot" install="github:lmctl-ai/lmplot@v0.3.1";
plot.data("map1", "5 4 3 1 0\n2 2 0 0 1\n0 0 0 1 0\n0 0 0 2 3\n0 1 2 4 3\n");
# The same values as x y z triples; rows are separated by blank lines.
plot.data("map2", "0 0 5\n0 1 4\n0 2 3\n0 3 1\n0 4 0\n\n1 0 2\n1 1 2\n1 2 0\n1 3 0\n1 4 1\n\n2 0 0\n2 1 0\n2 2 0\n2 3 1\n2 4 0\n\n3 0 0\n3 1 0\n3 2 0\n3 3 2\n3 4 3\n\n4 0 0\n4 1 1\n4 2 2\n4 3 4\n4 4 3\n\n");
plot.script("unset key\nset tic scale 0\nset palette rgbformula -7,2,-7\nset cbrange [0:5]");
plot.script("set cblabel 'Score'\nunset cbtics\nset xrange [-0.5:4.5]\nset yrange [-0.5:4.5]");
# Image 1: splot ... matrix with image, viewed as a map.
plot.png("images/heatmaps-1.png", 800, 600);
plot.cmd("set title 'Heat Map generated from a file containing Z values only'");
plot.cmd("set view map");
plot.cmd("splot '$map1' matrix with image");
plot.close();
# Image 2: image plus the non-zero values written as labels.
plot.png("images/heatmaps-2.png", 800, 600);
plot.cmd("set title 'Heat map with non-zero pixel values written as labels'");
plot.cmd("plot $map1 matrix using 1:2:3 with image, $map1 matrix using 1:2:($3 == 0 ? '' : sprintf('%g',$3)) with labels");
plot.close();
# Image 3: XYZ stream with 'plot ... with image'.
plot.png("images/heatmaps-3.png", 800, 600);
plot.cmd("set title 'Heat Map generated by plot from a stream of XYZ values'");
plot.cmd("plot '$map2' using 2:1:3 with image");
plot.close();
end _lmcode_
r = run_lmcode(p);
assert(r.ok, json_stringify(r));
cp = system cp images/heatmaps-2.png images/heatmaps.png;
assert(cp.code == 0);
println("wrote images/heatmaps.png (and images/heatmaps-1.png .. images/heatmaps-3.png)");