Search code examples
gnuplot

gnuplot - 2d interpolation, don't explicitly list x-values


I would like to generate using GNUPlot, a 2d plot from a list of y-axis values, without stating explicitly the corresponding x-value. In other words, I do not have

  x  y
  1  5
  2  3
  3  5
  4  6
  5  14

but instead have

5 10 3 5 6 14 (y-values listed only, not x values)

How can I generate a 2d plot from this, by specifying only the bounds of x, assuming uniforming spacing, and not explicitly listing x?

Then I would like to interpolate the y-values


Solution

  • You could use the 0 pseudocolumn for this:

       column(0)   The sequential order of each point within a data set.
                   The counter starts at 0 and is reset by two sequential blank
                   records.  The shorthand form $0 is available.
    

    In order to start counting from 1, the example below uses ($0+1). If needed, a more general form would be $0*step + xMin, where xMin denotes the required minimum value of x and step stands for the step size (spacing of individual x values).

    unset key
    
    $data <<EOD
    5
    10
    3
    5
    6
    14
    EOD
    
    fit (a*x + b) $data u ($0+1):1 via a,b
    
    plot \
        $data u ($0+1):1 w lp, \
        a*x + b w l lc rgb 'red' lw 2