Search code examples
gnuplot

Doing calculations in gnuplot's plot command


The following gnuplot code works good:

plot 'data.txt'  using 2:4  '   %lf %lf %lf %lf' title "1 PE" with linespoints;

In the following code, I want to say: "Use the number from column 4, but then divide it by the number from column 3". Or: "Use the number from column 2, but divide it by the constant 2.0". The following code demonstrates what I try to achieve, but it does not work.

plot 'data.txt'  using 2:4/4  '   %lf %lf %lf %lf' title "1 PE" with linespoints;
plot 'data.txt'  using 2:4/2.0  '   %lf %lf %lf %lf' title "1 PE" with linespoints;

Is something like this possible?


Solution

  • I don't usually work with datafiles formatted like this, but I think you're looking for something like:

    #divide column 4 by column 3
    plot 'data.txt'  using 2:($4/$3)  '   %lf %lf %lf %lf' title "1 PE" with linespoints
    
    #divide column 4 by constant 10.0
    plot 'data.txt'  using 2:($4/10.0)  '   %lf %lf %lf %lf' title "1 PE" with linespoints
    

    As a side note, I don't think there's any reason for passing the format portion to using here. Gnuplot splits the datafile on whitespace just fine:

    plot 'data.txt'  using 2:($4/$3) title "1 PE" with linespoints
    

    Should work just fine.