Skip to content

A6. Two axes and a model ​

Goal: the residual (log) and the drag coefficient (linear) on one plot, each on its own scale, and a model function over the data.

gnuplot
set logscale y
set ylabel 'continuity residual'
set y2label 'cd'
set ytics nomirror
set y2tics
gnuplot
plot 'run.dat' using 1:2 title 'continuity', \
     ''        using 1:5 axes x1y2 with points pt 7 ps 0.6 title 'cd', \
     0.30 + 0.25*exp(-x/25)*cos(x/6) axes x1y2 dt 2 title 'model'
wheel: zoom · drag: pan · right drag: box · a resetopen page ↗

Why it works ​

  • axes x1y2 plots an item against the second y axis, autoscaled on its items alone.
  • As in gnuplot, the y2 ticks are off until set y2tics; set ytics nomirror keeps the y ticks off the right border. set y2label, set y2range, set logscale y2 and set format y2 work as for y.
  • An item that is not a quoted file is a function of x, with the operators of using expressions. It is sampled at set samples points (100) over the x range of the data — here the iterations, 1 to 120 — and titled as written. Functions are drawn with lines unless with or set style function say otherwise.

Try this ​

  • fit f(x) 'run.dat' u 1:5 via a, b with f(x) = a + b*exp(-x/25): the model's parameters from the data, then plot ..., f(x) axes x1y2.
  • set link y2 via y*1e4 inverse y/1e4: a second axis that is the first one rescaled, gnuplot 6.0's way.

Pitfalls ​

  • Two axes on one plot are easy to misread: label both, and say in the key which axis a series uses (cd (right)).
  • A function samples the x range of the data: alone (plot sin(x)) it uses [-10:10], gnuplot's default.

Reference: second y axis, functions. Next: path B, watching a simulation.

foresight is available under your choice of license: GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT (License).