Gnuplot, normalize over x axis

5.7k views Asked by At

I've got two datasets referring to the same process executed in two different ways. The execution A is slower than the execution B with respect to real time, but the two graphs represent the same phenomena..

I can plot the two together as follows:

plot 'A' using 1:2, 'B' using 1:2

But I obtain two graphs with different X scales: A was slower, so the graph is much.

I can normalize the graph by doing the following:

plot 'A' using ($1 / maxA):2, 'B' using ($1 / maxB):2

Which works perfectly for me. The only problem being the maxA and maxB variables. They are trivial to determine (tail -n1 A | cut -f1 and tail -n1 B | cut -f1 respectively), but I was wondering if there's an automated way of doing it.

Thanks in advance for any kind answer.

Update

After I applied the excellent answer from Wrzlprmft, I finally got to the following pattern, which is quite convenient:

max(Source) = system('tail -n ' . Source . '| cut -f1')

A = 'path/to/A'
maxA = max(A)
plot A using ($1 / maxA):2

Another possible improvement could be including a Column parameter to the max function, so that we can also tune the param of the -f flag in cut.

Update

Changed my mind on acceptance, since the stats command seems to be better for this purpose.

Thanks everyone.

2

There are 2 answers

0
Karl On BEST ANSWER

Alternative is to use the stats command in gnuplot, without any external programs:

stats datafA using 1:2 name "A"
stats datafB using 1:2 name "B"

plot datafA using 1:2, datafB using ($1/B_max_x*A_max_x)

It also produces a lot more useful statistics of your data, check the variables it produces with show var A (or B, or STATS if you didn't supply a name).

OR (different solution), you plot B on the x2y1 coordinate system, where the x2 axis gets autoscaled independently.

set xtics nomirr
set x2tics 
set xrange [*:*] noextend # make sure the scaling is coherent
set x2range [*:*] noextend # by switching off extension
set link x2 via x*factor inverse x/factor # alternative, for gp>=v5.2
plot datafA us 1:2, datafB us 1:2 axes x2y1

If you know the relation between your abscissa values, you can directly link the two axes since gp 5.2

1
Wrzlprmft On

The system command allows you to execute programs and obtain their output. For example, you could use the following before your plot command:

maxA = system("tail -n1 A | cut -f1")
maxB = system("tail -n1 B | cut -f1")