Get ratio from 2 files in gnuplot

大憨熊 提交于 2019-12-17 04:07:45

问题


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

回答1:


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)




回答2:


The short answer is that... you cannot. Gnuplot processes one file at a time.

The work-around is to use an external tool, e.g. using the shell if you have a unix-like, or gnuplot.

join file1 file2 > merged_file will allow you to merge your files quite easily if the first column is identical in both files. Options allow to join on other columns and manage data missing in either file.

In case there is no common column but hte line number is relevant, paste will do.




回答3:


There is a trick if the two datasets don't fit (different sampling in x), but you have a good mathematical model for at least one of them:

fit f2(x) data2 us 1:2 via ...
set table $corr
plot data1 using 2:(f2($1))
unset table

plot $corr using 1:2

This is of course nonsense if both datasets have the same set of independent variables, because you can simply combine them (see other answers).



来源:https://stackoverflow.com/questions/20069641/get-ratio-from-2-files-in-gnuplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!