Search code examples
gnuplot

Extract Value from Spline Fit in Gnuplot


I know it is possible to draw a spline through data points in gnuplot using the command

plot 'data' u 1:2 smooth acsplines

but is it possible to evaluate the spline at an aribtrary point? ie. I'm looking for a functionality like

spline(1.54)

I could not find any information about this in the gnuplot documentation.


Solution

  • My first thought would be the following:

    • plot the acsplines into a table (check help table)
    • set samples large enough to have good precision (check help samples)
    • use stats to find the minimum distance from your desired value x0 (check help stats and show var STATS)
    • plot the data points, acplines and add a single point

    Script:

    ### find a point on acsplines curve
    reset session
    
    $Data <<EOD
    # x   y    weight
      0   4.0   10
      1   5.0  100
      2   3.0   10
      3   3.5    1
      4   2.0    1
      5   4.0    1
    EOD
    
    set table $acsplines
        set samples 1000
        plot $Data u 1:2:3 smooth acsplines
    unset table
    
    x0 = 1.54     # desired x-value
    stats $acsplines u 2:(abs($1-x0)) nooutput
    y0 = STATS_pos_min_y
    print myTitle=sprintf("x=%g, y=%.3f",x0,y0)
    
    plot $Data      u 1:2 w p pt 7 lc "red"  ti "points", \
         $acsplines u 1:2 w l      lc "blue" ti "acsplines", \
                '+' u (x0):(y0) every ::::0 w p pt 7 ps 2 lc "web-green" ti myTitle
    ### end of script
    

    Result:

    x=1.54, y=4.285
    

    enter image description here