Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

max - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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='')

To plot, use:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...