gnuplot

Interactive multiplot with shared x-axis


I often plot noisy data along with a model. The residuals are shown in a bottom panel:

set colors classic
set samp 20000

set table $data
    plot '+' u 1:(y=10*(sin(10*$1))):(y-invnorm(rand(0))) w e
unset table

set multiplot layout 2,1
plot $data us 1:($2+$3):3 w p pt 7 ps 0.5 t "noisy data", $data us 1:2 w l lc 3 t "model"
plot $data us 1:3 w p pt 7 ps .5 t "noise"
unset multiplot

However, this is not interactive anymore. After zooming one plot will be lost. (Matplotlib has here shared axis.)

My current approach is also not satisfying, but works at least if zooming only horizontally.

set autoscale y2fix 
set y2range [-5:25]
plot [][-30:] $data us 1:($2+$3):3 w p pt 7 ps 0.5 t "noisy data", $data us 1:2 w l lc 3 t "model", $data us 1:3 w p pt 7 ps .5 t "noise" axis x1y2

enter image description here Are there better ways?


Solution

  • Based on the suggestion by @theozh to redraw everything in a while loop, this version reacts on mouse click. Each left click redraws the multiplot.

    set colors classic
    set samp 20000
    set table $data
        plot '+' u 1:(y=10*(sin(10*$1))):(y-invnorm(rand(0))) w e
    unset table
    
    cond=1; while(cond) {
       set yrange [*:*]
       set multiplot layout 2,1 upwards
       unset label 1
       plot $data us 1:3 w p pt 7 ps .5 t 'noise'
       set label 1 at graph 0.01, graph 0.95 "middle click = stop" boxed front
       plot $data us 1:($2+$3):3 w p pt 7 ps 0.5 t 'noisy data', $data us 1:2 w l lc 3 t 'model'
       unset multiplot
    
       pause mouse button1,button2
       cond = MOUSE_KEY != 2   # exit on middle click
    }
    

    Starting rectangular zoom with right click and finishing it with left click, it appears still smooth, though not as speedy as the version with axis x1y2. Since bind does unfortunately not work for mouse events, pause and while loop is used instead to emulate this behaviour.