Gnuplot: data normalization

安稳与你 提交于 2021-01-28 05:30:14

问题


I have several time-based datasets which are of very different scale, e. g.

[set 1]
2010-01-01  10
2010-02-01  12
2010-03-01  13
2010-04-01  19
…

[set 2]
2010-01-01  920
2010-02-01  997
2010-03-01  1010
2010-04-01  1043
…

I'd like to plot the relative growth of both since 2010-01-01. To put both curves on the same graph I have to normalize them. So I basically need to pick the first Y value and use it as a weight:

plot "./set1" using 1:($2/10), "./set2" using 1:($2/920)

But I want to do it automatically instead of hard-coding 10 and 920 as dividers. I don't even need the max value of the second column, I just want to pick the first value or, better, a value for a given date.

So my question: is there a way to parametrize the value of a given column which corresponds a given value of the given X column (X is a time axis)? Something like

plot "./set1" using 1:($2/$2($1="2010-01-01")), "./set2" using 1:($2/$2($1="2010-01-01"))

where $2($1="2010-01-01") is the feature I'm looking for.


回答1:


Picking the first value is quite easy. Simply remember its value and divide all data values by it:

ref = 0
plot "./set1" using 1:(ref = ($0 == 0 ? $2 : ref), $2/ref),\
     "./set2" using 1:(ref = ($0 == 0 ? $2 : ref), $2/ref)

Using the value at a given date is more involved:

Using an external tool (awk)

ref1 = system('awk ''$1 == "2010-01-01" { print $2; exit; }'' set1')
ref2 = system('awk ''$1 == "2010-01-01" { print $2; exit; }'' set1')
plot "./set1" using 1:($2/ref1), "./set1" using 1:($2/ref2)

Using gnuplot

You can use gnuplot's stats command to pick the desired value, but you must pay attention to do all time settings only after that:

a) String comparison

stats "./set1" using (strcol(1) eq "2010-01-01" ? $2 : 1/0)
ref1 = STATS_max
...
set timefmt ...
set xdata time 
...
plot ...

b) Compare the actual time value (works like this only since version 5.0):

reftime = strptime("%Y-%m-%d", "2010-01-01")
stats "./set1" using (timecolumn(1, "%Y-%m-%d") == reftime ? $2 : 1/0)
ref1 = STATS_max
...
set timefmt ...
set xdata time 
...
plot ...


来源:https://stackoverflow.com/questions/43669052/gnuplot-data-normalization

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