Search code examples
bashgnuplot

Ploting data with gnuplot


I have 3 columns and 1000 rows of integers in .dat file and I have to plot it in the graph in the way that first column is on the x-axes and sqrt(c2²+c3²) is on the y-axes, where c2 is from the second column and c3 is from the third column, using gnuplot script.

Normally I use something like plot <somefile.dat> using 1:2 but now I have to use second and third column somehow like that using 1:sqrt(2²+3²).


Solution

  • To construct equations from column values from your datafile, gnuplot provides a parenthesis grouping, e.g. (your equation here). In order to define your equation within parenthesis, you refer to the column value wanted by prefixing the column number with a '$' (e.g. $2 refers to the value from column 2, $3 refers to the value from column 3, etc..) and you can use those references as many times as needed within the parenthesis and each use will be replaced by the value from the numbered column.

    In your case to have the 1st column be your independent x-values and your equation result the dependent value drawing the numbers from columns 2 & 3, you can do:

    plot "somefile.dat" using 1:(sqrt($2*$2+$3*$3))
    

    A short example with the input file as:

    $ cat somefile.dat
    1 1 1
    2 2 2
    3 3 3
    4 4 4
    5 5 5
    6 6 6
    7 7 7
    8 8 8
    9 9 9
    10 10 10
    

    Creating a short plot file for convenience:

    $ cat some.plt
    plot "somefile.dat" using 1:(sqrt($2*$2+$3*$3))
    

    You can generate your plot with

    $ gnuplot -p some.plt
    

    enter image description here

    Look things over and let me know if this is what you needed.