I have a script that converts raw data for interactive use with gnuplot. The scipt takes arguments that let me apply some filtering on the data. In principle, it could be emulated by this script:
import time, sys
print('Generating data...', file=sys.stderr)
time.sleep(1)
print('x', '100*x', 'x**3', '2**x')
for x in range(*map(int, sys.argv[1:3])):
print(x, 100*x, x**3, 2**x)
I plot the series of data in columns by piping the shell command to gnuplot and iterating over columns:
gnuplot> plot for [i=2:4] '< python3 pipe_data.py 1 11' u 1:i w l t columnhead(i)
Generating data...
Generating data...
Generating data...
gnuplot>
Currently, when I notice that the script takes too long to run it multiple times, I execute it outside of gnuplot and save the output to a file. However, it is cumbersome having to do it whenever I want to change the arguments to the script.
I would like gnuplot to execute '< python3 pipe_data.py'
only once, so that only one Generating data...
is printed on the screen. Is this possible?
Ideally, gnuplot would cache contents of special filenames starting with a <
. This way it would be possible to tweak the appearance of the plot, without regenerating the data, e.g.:
gnuplot> plot for [i=2:4] '< python3 pipe_data.py 1 11' u 1:i w l t columnhead(i)
Generating data...
gnuplot> plot for [i=2:4] '< python3 pipe_data.py 1 11' u 1:i w lp t columnhead(i)
gnuplot> plot for [i=2:4] '< python3 pipe_data.py 5 12' u 1:i w lp t columnhead(i)
Generating data...
gnuplot> plot for [i=2:4] '< python3 pipe_data.py 1 11' u 1:i w p t columnhead(i)
gnuplot>
This could become problematic when the raw data changes, gnuplot would have no way of knowing this. But I still hope there is some way to achieve this effect. If not with just gnuplot, then maybe with some external tools?
For the record, I use gnuplot v4.6.
I came up with a bash script that solves all the issues for me:
I named it
$
(for cash/cache),chmod u+x
'd it, and placed it withinPATH
:Now I can take advantage of it using
<$
instead of<
:The following command should do what you want. I am generating a ona-data-point file that takes two parameters,
i
andj
. The file is generated automatically when you callplot data(i,j)
and then it's reused every subsequent time. Change mysleep 5; echo %i %i
by your command. You'll also need to change the formats if you're not using integers.Example usage: