gnuplot gallery · examples/nonlinear2.lms
Nonlinear axes
logscale versus set nonlinear (gnuplot nonlinear2.dem)
Loading the WebAssembly host…
What runs
The page fetches examples/nonlinear2.bin, 2,788 bytes of lmcode bytecode
(sha256 58dd5502f7df…), 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.
# Log-scaled and nonlinear axes: gnuplot "nonlinear2.dem".
# The same Bode-style magnitude/phase plot twice: once with the classic
# 'set logscale', once with the equivalent 'set nonlinear ... via ... inverse ...'.
# Image 1: logscale. Image 2: nonlinear axes (identical output expected).
mkdir("images");
mkdir("build");
p = _lmcode_ cli="lmplot" install="github:lmctl-ai/lmplot@v0.3.1";
# common setup: complex transfer function A(jw), a second y axis for the phase
plot.cmd("set dummy jw, y");
plot.cmd("set grid xtics ytics");
plot.cmd("set key inside center bottom vertical Right noreverse enhanced autotitle box");
plot.cmd("set samples 160, 160");
plot.cmd("set style data lines");
plot.cmd("set xtics border out scale 1,0.5 nomirror");
plot.cmd("set ytics border out scale 1,0.5 nomirror");
plot.cmd("set ytics norangelimit 0.1 textcolor rgb 'dark-violet'");
plot.cmd("set y2tics border out scale 1,0.5 nomirror");
plot.cmd("set y2tics norangelimit autofreq textcolor rgb '#56b4e9'");
plot.cmd("set xlabel 'jw (radians)'");
plot.cmd("set ylabel 'magnitude of A(jw)'");
plot.cmd("set y2label 'Phase of A(jw) (degrees)'");
plot.cmd("set xrange [ 1.1 : 90000.0 ] noextend");
plot.cmd("set yrange [ 0.1 : 1.0 ]");
plot.cmd("A(jw) = ({0,1}*jw/({0,1}*jw+p1)) * (1/(1+{0,1}*jw/p2))");
plot.cmd("p1 = 10");
plot.cmd("p2 = 10000");
# --- 1: classic logscale ---
plot.png("images/nonlinear2-1.png", 800, 600);
plot.cmd("set title 'Log-scaled axes defined using \"set log\"'");
plot.cmd("set label 1 'This version of the plot uses\nset logscale x\nset logscale y' at graph 0.5, 0.85, 0 center norotate back nopoint");
plot.cmd("set log x");
plot.cmd("set log y");
plot.cmd("set ytics nolog");
plot.cmd("plot abs(A(jw)) lt 1, 180/pi*arg(A(jw)) axes x1y2 lt 3");
plot.close();
# --- 2: the same through set nonlinear ---
plot.png("images/nonlinear2-2.png", 800, 600);
plot.cmd("unset log");
plot.cmd("set title 'Log-scaled axes defined using \"set nonlinear\"'");
plot.cmd("set label 1 'The same plot except that \"set logscale\" is replaced by\nset nonlinear x via log10(x) inverse 10**x\nset nonlinear y via log10(y) inverse 10**y'");
plot.cmd("set nonlinear x via log10(x) inverse 10**x");
plot.cmd("set nonlinear y via log10(y) inverse 10**y");
plot.cmd("set xtics log");
plot.cmd("set ytics nolog");
plot.cmd("plot abs(A(jw)) lt 1, 180/pi*arg(A(jw)) axes x1y2 lt 3");
plot.close();
end _lmcode_
r = run_lmcode(p);
assert(r.ok, json_stringify(r));
cp = system cp images/nonlinear2-1.png images/nonlinear2.png;
assert(cp.code == 0);
println("wrote images/nonlinear2.png (and images/nonlinear2-1.png .. images/nonlinear2-2.png)");