Wednesday, April 12, 2006

graph plotting with gnuplot

Gnuplot is a free, command-driven, interactive, function and data plotting program.
Here i' m providing a quick reference guide for plotting graphs (as many of us don't bother to go in detailed documentation).
On Unix/Linux systems start Gnuplot by simply typing:
gnuplot

first the formal syntax :

plot {[ranges]}
{[function] | {"[datafile]" {datafile-modifiers}}}
{axes [axes] } { [title-spec] } {with [style] }
{, {definitions,} [function] ...}

To plot functions simply type: plot [function] at the gnuplot> prompt
gnuplot> plot sin(x)
gnuplot> splot sin(x*y/20)
gnuplot> plot sin(x) title 'Sine Function', tan(x) title 'Tangent'


Now for plotting data points firstly data points should be written in text file with axis ranges as columns.Data files should have the data arranged in columns of numbers. Columns should be separated by white space (tabs or spaces) only, (no commas). Lines beginning with a # character are treated as comments and are ignored by Gnuplot. A blank line in the data file results in a break in the line connecting data points. '
Plotting a graph from data file "plot.data" , do
gnuplot>plot "plot.data"

Now customizing the plot :



set xlabel "x-axis data"

# will set a label "x-axis data" in graph
set ylabel "y-axis data"

set title "x vs y"
# will set title for graph
set xrange [0.001:0.005]
# Change the x-axis range:
set yrange [20:500]
# Change the y-axis range:
set logscale
# plot using log scale
set autoscale
#let gnuplot determine ranges
plot "file.data" with lines
#will join data points with lines
plot "file.data" smooth csplines with lines
#will join plot graph using smooth curves
Gnuplot can mathematically modify your data column by column:
to plot sin( col.3 + col.1 ) vs. 3 * col.2 type:

plot 'force.dat' using (3*$2):(sin($3+$1))

To print graph (this will store graph in post script(.ps) file do this before plotting graph:

set output "graphs.ps"
set terminal postscript
plot "file.data" using lines


This will save the graph in graph.ps
For more help type 'help' in gnuplot mode :
gnuplot>help


somelinks
www.duke.edu/~hpgavin/gnuplot.html
A simple guide to GNU plot
Introduction to GNU plot

3 comments:

Anu said...

Thanks for the post on Gnuplot! It was quick and easy :)

pkj said...

I saw your posting regarding Gnuplot, and thought you might be interested to know that there is now a book on it: "Gnuplot in Action". You can pre-order it directly from the publisher: Manning: Gnuplot in Action.

If you want to learn more about the book and the author, check out my book page at Principal Value - Gnuplot in Action.

Anonymous said...

Here you can find a pdf version of the not so faq:
http://www.gerolf.ziegenhain.com/linux/content.html#configuration

:)