Wednesday, October 3, 2012

BASH: Plot graph with one line command using gnuplot

This is for a quick visual inspection of your job data output as XY plot. The XY data containing is read from the file data_input.dat

    1.00    0.00000
    2.00    0.74103
    3.00    0.79751
      ..     ....
      ..     ....

$ gnuplot
> plot "data_input.dat"

Now a scatter plot will pop up as a window. If you are logged into remote session with ssh, make sure you have forwarded the X. i.e.  logged in with -X flag

$ ssh -X yourhost

Change it to line plot by adding 'with lines'

$ gnuplot
> plot "data_input.dat" with lines

__________________________________________________________

Now for more advanced options:
1) To save the plot as png image:
add set terminal png, set output "plot.png"

-----------------------------------------------------------------------------
set terminal png
set output "plot.png"
plot "data_input.dat" with lines
-----------------------------------------------------------------------------
Now the plot will be saved as 'plot.png' into working directory.
You can open the image from terminal using  eog (eye of gnome)
$ eog plot.png

2) To label the X and Y and plot grid:
add set ylabel "RMS", set xlabel "Time" and set grid
-----------------------------------------------------------------------------
set terminal png
set output "plot.png"
set ylabel "RMS"
set xlabel "Time"
set grid
plot "data_input.dat" with lines
-----------------------------------------------------------------------------

3)  If you want to plot 1st and 5th column in your data_input.dat:
add 'using 1:5' in plot command
 

-----------------------------------------------------------------------------
set terminal png
set output "plot.png"
set ylabel "RMS"
set xlabel "Time"
set grid
plot "data_input.dat" using 1:5 with lines
-----------------------------------------------------------------------------

4) To plot two data series in the same plot and label each line:
add next series after a comma


-----------------------------------------------------------------------------
set terminal png
set output "plot.png"
set ylabel "RMS"
set xlabel "Time"
set grid
plot "data_input.dat" using 1:2 with lines title 'data-1',  "data_input.dat" using 1:5 with lines title 'data-5'
-----------------------------------------------------------------------------

 To plot many series, it's convenient to use the line continuation character, "\" .
-----------------------------------------------------------------------------
set terminal png
set output "plot.png"
set ylabel "RMS"
set xlabel "Time"
set grid
plot "data_input.dat" using 1:2 with lines title 'data-1', \
       "data_input.dat" using 1:3 with lines title 'data-3'  \
       "data_input.dat" using 1:5 with lines title 'data-5'  \
-----------------------------------------------------------------------------

5) Running it from a script

Put all these commands into a script and run if you have to run them several times

-----------------------------------------------------------------------------
#!/usr/bin/gnuplot
reset
set terminal png
set output "plot.png"
set ylabel "RMS"
set xlabel "Time"
set grid
plot "data_input.dat" using 1:2 with lines title 'data-1', \
       "data_input.dat" using 1:3 with lines title 'data-3'  \
       "data_input.dat" using 1:5 with lines title 'data-5'  \
-----------------------------------------------------------------------------

matplotlib is another even better option for advanced graph creation

More examples:
Gnuplot 4.2 Tutorial
http://www.duke.edu/~hpgavin/gnuplot.html

Plot your graphs with command line gnuplot » Linux by Examples
http://linux.byexamples.com/archives/487/plot-your-graphs-with-command-line-gnuplot/

Data visualization tools for Linux
http://www.ibm.com/developerworks/linux/library/l-datavistools/

No comments:

Post a Comment