Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnuplot: plotting the maximum of two files

Tags:

max

gnuplot

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

like image 208
Marco Pietrosanto Avatar asked May 25 '26 21:05

Marco Pietrosanto


1 Answers

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))
like image 178
Christoph Avatar answered May 30 '26 07:05

Christoph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!