Search code examples
maxgnuplot

Gnuplot: plotting the maximum of two files


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


Solution

  • 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))