Sunday, December 5, 2010

Plotting graphs for scientific papers with gnuplot

I was writing our first IEEE grade paper few days back. It was first written using MS word as my text editor. Now that it got selected to be published on the IEEE explore, we thought of preparing a latex based paper for final publication.
One of the major proplem was the image quality of some of the figures. I tackled that using the popular open source vector grphics editor - > Inkscape. With Inkscape you can prepare some high quality images.
The next problem was with the graph quality, which had been drawn using MS excel. The solution was to use GNUPlot tool. It is a nice tool especially crafted for complex scientific drawings. I used it as a simple data plotter. But it is capable of plotting scientific functions and many more things.
So here is how to plot a simple graph if have the plotting data available with you. Installing the program is pretty trivial. ;)  

Preparing the data file : plot.dat
you have to prepare a data file, that to be used by the gnuplot. Data should be delimited by a common delimiter. The default delimiter is the 'space'. Here is what a sample data file would look like.

writing gnuplot script : plot.script
it is possible to use gnuplot tool in the interactive mode. But is is often useful to store your command as a script file so that you can call the script file over and over again.

set xlabel "number of nodes" font "Times-New Roman,16"
set ylabel "speedup ratio" font "Times-New Roman,16"
set xrange [0:24]
set yrange [0:25]
set xtics out  0,3,24
set mxtics 3
set ytics in  0,5,25
set mytics 5
set key 9,22
plot 'plot.dat' using 1:2 title 'actual speedup' with linespoints , \
'plot.dat' using 1:3 title 'ideal speedup' with lines

The most of the above commands are self explanatory, such as xlabel, xrange, etc.
set xtics instruct the program to set the labeling number after each 3 units. The set mxtics instructs to place subdivisions within the each unit.
Generating the graph
open up a terminal and type gnuplot. assuming you have already installed gnuplot in your machine, you should see the interactive gnuplot terminal. Give the following command to prepare the the graph using our script file. 

gnuplot> call './plot.script'   

This should spawn up a new window containing our graph. :)


Gnuplot uses the concept of drivers to output its generated graphs. Here I have used the output of default driver in the above figure. By changing the terminal driver we can print directly to a printer, to a command shell or to a postscript file. When you are preparing the the graphs for a latex document you got to specify your output terminal as the postscript (ps). The generated .ps file, can be later converted in to ps2pdf utility in UNIX. The converted .pdf image can be inserted in to your latex document and can be compiled with pdfLatex.
I have attached the plot.dat file and the plot.script file I used to generate the above plot.  

No comments:

Post a Comment