3

let's assume I have two files formatted like this:
x --- y
0 --- 2
1 --- 2.4
2 --- 3.6
which differ for the values of y. is there a way to plot a single graph that is, for every x, the maximum value of y between the two files?

Dunno if explained my self sufficiently well.

I was trying with conditional sentences but I couldn't find any expression that let me search in 2 different files

2 Answers 2

4

There is no way to combine two files or more in a single plot with gnuplot only. You must use an external tool to do this, e.g. the command line utility paste:

max(x, y) = (x > y ? x : y)
plot '< paste fileA.txt fileB.txt' using 1:(max($2, $4))

The y values are contained in the second and fourth columns.

This next version uses a python script with numpy to concatenate the files, but any other scripting language would also do:

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

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

A = np.loadtxt(sys.argv[1])
B = np.loadtxt(sys.argv[2])

np.savetxt(sys.stdout, np.c_[A, B], delimiter='\t')

To plot, use:

max(x, y) = (x > y ? x : y)
plot '< python paste.py fileA.txt fileB.txt' using 1:(max($2, $4))
3
  • Can you provide a platform-idependent solution too? Sep 29, 2013 at 23:21
  • 1
    @DanielTuzes I thinks it is not possible to do it with gnuplot only, which would be the best way. I added a python script, which might be more platform-independent than using paste, see the edit.
    – Christoph
    Sep 30, 2013 at 7:34
  • 1
    @DanielTuzes just for fun... Find some platform-independent gnuplot only versions in my answer.
    – theozh
    Feb 2, 2022 at 10:05
1

Just for the records, there is a way with gnuplot only to get the maximum out of two files.

For sure, it's probably more efficient to use Linux tools or on Windows install, e.g. CoreUtils from GnuWin, but with gnuplot-only you are surely platform independent without extra installations.

Assumption: both files have same number of lines and identical x-values

Edit: simplified code which works for all gnuplot versions>=4.6.0 and faster version for gnuplot>=5.2.0 using an array.

The simple "trick" is to write the y value of one file into a single string and address them via word(). For small data this is ok, but for larger data (>10'000 lines) it might get slow because apparently it runs with something like O(N^2). Just to get an idea (on my system): 1'000 lines take 0.4 seconds, 10'000 lines take 13 seconds and 20'000 lines already take 45 seconds.

As comparison, the array-solution for gnuplot>=5.2.0 just takes about 3 seconds for 10'000 lines.

Data:

SO19079146_1.dat

1   1.1
2   2.1
4   1.5
6   1.3
7   0.2
8   1.5
9   2.1

SO19079146_2.dat


1   2.1
2   2.5
4   1.5
6   0.3
7   0.7
8   1.0
9   1.4

Script 1: (works for gnuplot>=4.6.0, March 2012)

### plot maximum from two files
reset

FILE1 = 'SO19079146_1.dat'
FILE2 = 'SO19079146_2.dat'

data2 = ''
stats FILE2 u (data2=data2.' '.sprintf("%g",$2)) nooutput

set offset 1,1,1,1
max(col) = (i=int(column(0)+1), y1=column(col), y2=real(word(data2,i)), y1>y2 ?  y1 : y2)

plot FILE1 u 1:(max(2)) w lp pt 7 lw 8 lc rgb "grey" ti "Max", \
        '' u 1:2 w lp pt 7 lc rgb "red"  ti "Data1", \
     FILE2 u 1:2 w lp pt 7 lc rgb "blue" ti "Data2"
### end of script

Script 2: (works for gnuplot>=5.2.0, Sept. 2017)

### find the maximum out of two files/datablocks (gnuplot>=5.2.0)
reset session

FILE1 = 'SO/SO19079146_1.dat'
FILE2 = 'SO/SO19079146_2.dat'

stats FILE1 u 0 nooutput
array A[STATS_records]
stats FILE2 u (i=int($0+1), A[i]=$2) nooutput

set offset 1,1,1,1
max(col) = (i=int(column(0)+1), y1=column(col), y2=A[i], y1>y2 ?  y1 : y2)

plot FILE1 u 1:(max(2)) w lp pt 7 lw 8 lc "grey" ti "Max", \
        '' u 1:2 w lp pt 7 lc "red"  ti "Data1", \
     FILE2 u 1:2 w lp pt 7 lc "blue" ti "Data2"
### end of script

Result: (identical for all above versions)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.