3
votes

I would like to pipe data into gnuplot and plot it without entering the gnuplot command line or providing an existing script file. Eg a one-liner such as cat datafile | gnuplot and see the plot come up. Suppose the datafile is well formatted, such as two simple columns of numerical data separated by a tab or space.

As is this would close immediately even if it would work (which it doesn't) but this can be resolved with the -persist option (abbreviated -p).

Essentially I would like the following line of code to work, or to understand why it doesn't and how to modify it to work: echo -e "1 3\n2 4\n3 5" | gnuplot -p <<< "plot '-'"

I can get something working using the -e option, but this seems to often break for me once I start making more complex plotting commands and I don't understand exactly what that option is doing, so I would prefer to simply write a short script file using a heredoc, as attempted above. This works: echo -e "1 3\n2 4\n3 5" | gnuplot -p -e "plot '-'"

Why doesn't the column data that is piped into gnuplot simply plot, even when using the plot '-' syntax to request that plot take the data in from stdin?

1
The -e tells gnuplot to execute the following code. As simple as this. I don't understand what your actual problem is: you have a working snippet. - Christoph
I don't want to depend on the -e option, that's the idea. I'd rather just use an actual script file but in the form of a heredocument. The code should then execute from the heredoc gnuplot commands, rather than from -e; but this doesn't work. This is what I would like to understand and make work. Thanks - Daniel Naftalovich
Rather actually the issue is that I am trying to understand how to pipe data in when feeding a script as a heredoc. Eg the following works gnuplot -p <<< script but when also piping in data like the following it fails cat data | gnuplot -p <<< script. Is there a way to use a heredoc script and pipe data at the same time? - Daniel Naftalovich
create an alias or a function, something like alias gp="gnuplot -p -e \"plot '-'\" ". Then you can use it as cat 'data.dat' | gp. Alternatively, you can create a gnuplot script and place it in /usr/bin/ or /usr/local/bin/ - vagoberto

1 Answers

0
votes

You want to pipe 2 different streams to gnuplot: data and plot commands. Gnuplot takes just one. Then why not simply concatenate the plot commands and data?

cat > file << .
plot '-' using (\$1):(\$2)
1 2
2 3
3 4
e
.

and then cat file | gnuplot -p.

Remember to escape the $ characters if they are gnuplot variables and not shell variables.