Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe plot data to gnuplot script

Tags:

gnuplot

People also ask

How use gnuplot to plot data from a file?

To plot functions simply type: plot [function] at the gnuplot> prompt. Discrete data contained in a file can be displayed by specifying the name of the data file (enclosed in quotes) on the plot or splot command line. Data files should have the data arranged in columns of numbers.

Why gnuplot is used?

Gnuplot is used as a back-end graphics driver by higher-level mathematical packages such as Octave. Gnuplot can be wrapped in a cgi script for use as a web-driven plot generator. Gnuplot supports context- or data-driven flow control and iteration using familiar statements if else continue break while for.

What is plot in gnuplot?

plot is the primary command for drawing plots with gnuplot. It creates plots of functions and data in many, many ways. plot is used to draw 2-d functions and data; splot draws 2-d projections of 3-d surfaces and data. plot and splot contain many common features; see splot (p. ) for differences.

What is gnuplot Ubuntu?

DESCRIPTION. Gnuplot is a command-driven interactive plotting program. If file names are given on the command line, gnuplot loads each file with the load command, in the order specified, and exits after the last file is processed. If no files are given, gnuplot prompts for interactive commands.


If you are on a Unix system (i.e. not Windows) you can use '<cat' instead of '-' to read from stdin:

plot '<cat' using ...

Then you can do cat data.txt | gnuplot script.gp. However, in the specific case you mention in your question, with the plot in the for loop, you read the input three times. So sending the data through stdin is not appropriate, since the data will be gone after the first time it is read.


not a direct answer but this is what i use to quickly look at data. it's especially helpful with the cut command

cat data.txt | cut -f2 -d' ' | gnuplot -p -e "plot '<cat'"

What's wrong with using the -e option of gnuplot from shell? You can provide a variable as input, say data.txt, from shell using:

gnuplot -e "filename='data.txt';ofilename='test.png'" plot.gnu

You should be able to call the above command multiple times with different values for "filename" from shell using a for loop.

And then you change your script plot.gnu to:

set terminal png
set output ofilename
plot for[col=2:4] filename using 1:col title columnheader(col) with lines

If you want to plot data coming from a pipe more than once, you need to store it somehow in memory. My preferred way is to use a temporary file in /dev/shm, which exists in most Linux systems and maps to RAM. Just to keep things clean, I set a trap to delete the temporary file at exit.

Example (using your data.txt):

cat data.txt | (cat > /dev/shm/mytempfile && trap 'rm /dev/shm/mytempfile' EXIT && gnuplot -e "set terminal dumb; plot for[col=2:4] '/dev/shm/mytempfile' using 1:col title columnheader(col) with lines")

Result:

12 ++------------+-------------+-------------+-------------+------------**
   +             +             +             +             + Best ****** +
   |                                                        Worst***#### |
10 ++                                              *******Average $$$$$$++
   |                                           ****                      |
   |                                        ***    $$$$               $$$$
 8 ++                                     **     $$    $$        $$$$$  ++
   |                                    **     $$        $$    $$        |
   |                               *****    $$$              $$       ####
 6 ++                          ****       $$ ############# $$    #####  ++
   |                         **         $$ ##             #  ####        |
   |                       **        $$$ ##                ##            |
   |                     **      $$$$  ##                                |
 4 ++         ***********   $$$$$  ####                                 ++
   |     *****  ###################                                      |
   | ****   $$##                                                         |
 2 **    $$$##                                                          ++
   #########                                                             |
   + $$          +             +             +             +             +
 0 $$------------+-------------+-------------+-------------+------------++
   0             2             4             6             8             10

How about using the system() command

set terminal png
set output "test.png"

# read shell input
# the echo prints the variable, which is then piped to gnuplot
fname = system("read filename; echo $filename")

plot for[col=2:4] fname using 1:col title columnheader(col) with lines 

You can call it now with

echo "data.txt" | gnuplot script.gp