Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ratio from 2 files in gnuplot

Tags:

gnuplot

I have 2 dat files:

a.dat
#Xs
100   25
200   56
300   75
400   67


b.dat
#Xs
100   65
200   89
300   102
400   167

I want to draw a graph in the gnuplot where the yy values are a ratio between the values of a.dat and b.dat respectively. e.g., 25/65, 56/89, 75/102, and 67/167.

How I do this? I only know make a plot like this, and not with the ratio.

plot "a.dat" using 1:2 with linespoints notitle
     "b.dat" using 1:2 with linespoints notitle
like image 849
xeon Avatar asked Nov 19 '13 10:11

xeon


People also ask

How use gnuplot to plot data from a file?

To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.

Does gnuplot support multiple Y axes on a single plot?

5.9 Does gnuplot support multiple y-axes on a single plot? Yes. 2D plots can have separate x axes at the bottom (x1) and top (x2), and separate y axes at the left (y1) and right (y2).

Is gnuplot easy?

Running gnuplot is easy: from a command prompt on any system, type gnuplot. It is even possible to do this over a telnet or ssh connection, and preview the graphs in text mode!

What files can gnuplot open?

Gnuplot can read binary data files. However, adequate information about details of the file format must be given on the command line or extracted from the file itself for a supported binary filetype. In particular, there are two structures for binary files, a matrix binary format and a general binary format.


1 Answers

You cannot combine the data from two different files in a single using statement. You must combine the two files with an external tool.

The easiest way is to use paste:

plot '< paste a.dat b.dat' using 1:($2/$4) with linespoints

For a platform-independent solution you could use e.g. the following python script, which in this case does the same:

"""paste.py: merge lines of two files."""
import sys

if (len(sys.argv) < 3):
    raise RuntimeError('Need two files')

with open(sys.argv[1]) as f1:
    with open(sys.argv[2]) as f2:
        for line in zip(f1, f2):
            print line[0].strip()+' '+line[1],

And then call

plot '< python paste.py a.dat b.dat' using 1:($2/$4) w lp

(see also Gnuplot: plotting the maximum of two files)

like image 191
Christoph Avatar answered Oct 27 '22 02:10

Christoph