GNUPlot, Latek and my New Year Resolutions

One of my new year's resolutions was to be meticulous about everything i started and worked on this year. So, when i started learning GNUPlot and working on Latek for my lab reports, i got right down to business.

There are some people in this world who can remember a lot of things, specifically coding i.e the syntax as to how to use the for/if-else loops in C, writing data in columns in Latek and setting limits on GNUPlot. I am not one of those persons so i knew the minute i started working with these software that i needed a way to easily recollect these things if and when needed! I didn't want to write them all down on a notebook because well, it's just old school.
I thought that it'd be a better idea to have a template file with all of the basic commands noted down, the syntax mentioned and usage described. This way, when i needed something, i could just copy the commands from the template file and use them.

I'm sure that i'm not the first person to be following this method! Heck, i did the same thing when i started learning Latek and i even wrote about the template file i use for Latek here on my blog.

What is new is the template (i know that it's not the right word but still, i like using it) file for GNUPlot.


==========================================================

GNUPlot is an easy to use(or atleast that's what i feel right now) and open source plotting tool used for (relatively) professional looking graphs (better than the ones produced by excel i mean). Well, i can vouch for it's ease in learning because i've been able to pick up the most basic and common tools over a weekend. 
Let me give you a hint of the basic commands... 

in a terminal, invoke GNUPlot to start the program. 
the prompt will now read 
"gnuplot>"

you can type 'quit', 'exit' or use 'ctrl+c' to exit the program. 

moving on, the default output setting is an X11 window. 
if you want to change the output type and save the plot, do the following - 

gnuplot> set terminal postscript enhanced
gnuplot> set terminal latex
gnuplot> set term dumb
gnuplot> set output 'ex1.eps'
gnuplot> set output 'ex1.tex'

the postscript option gives .ps type output files and the enhanced version gives output files which have latex type typesetting features in them. Use of the enhanced option for all the different output is advised. 
Also, it is to be noted that while using these options, there will be no window displaying the plot, it will directly be saved as a file.
the other kinds of output options are - 

gnuplot> png enhanced
gnuplot> X11 enhanced - will set the output back to it's original form, in an X11 window
another addition to this would be the use of 
gnuplot> png enhanced size a,b - sets the size of the output file. 

a short bash script can be used to convert .ps type files to .png files
$ pstoimg -scale 2 test.eps or test.ps

moving ahead, now that we know the kinds of output options, let's set it back to the X11 window mode for now so that we can check how the plots look. 

And before we move on, one other important thing. 
If you didn't know before hand, GNUPlot can be used to extract and plot data from files. And inorder to be able to do this, you need to invoke GNUPlot from directory in which your data files are present i.e you need to change your current directory in the terminal to the directory with the data files. If you don't know how to change your working directory, google it! 

gnuplot> plot "data.dat" using 1:2 title 'example plot' with linespoints

where data.dat is the file from which i'm extracting data to be plotted. 
the data that i wish to plot is in the 1st and 2nd columns in the data file which is why we mention 

gnuplot> ...... ........ using 1:2 ...... .....
if you wish to plot data from the nth and the mth columns, you should use 

gnuplot> ...... ........ using n:m ...... ..... 
Data from the nth column are plotted on the x-axis and data from the mth column are plotted on the y-axis. 

the option "linespoints" at the end of the command refers to how the plot looks. a few other options are 'lines', 'points'. you can look up more such options using the help tool.

A short form of this is - 
gnuplot> plot "data.dat" u 1:2 
in this case, the x and y range are autoset. If you wish to set the x and y ranges on your own, 

'#' is used for comments in gnuplot scripts
gnuplot> set datafile commentschars '..'
defines the characters used to comment in datafiles so that they wouldn't be confused with. 

gnuplot> set xrange [... : ...]
gnuplot> set yrange [... : ...]
(or) 
set xr [.. : ..]
set yr [.. : ..]

you can revert back to the autoscaling option using 
gnuplot> set autoscale

GNUPlot also allows you to set a logscale on the axes 
gnuplot> set logscale x/y

Now that your plot is complete, it's time to label the axes and the plot itself. 
gnuplot> set xlabel "..."
gnuplot> set ylabel "..."
gnuplot> set title "..."

well, to add finer details to the plot, we add finer markings on the x and y axis
gnuplot> set xtics a,b # starts the xtics at points a and increments by b. or 
gnuplot> set xtics (a,b,c,d) # sets the xtics to the specific points a,b,c,d
gnuplot> set xtics auto
gnuplot> set ytics auto
gnuplot> set mxtics
# correspond to the points on the x and y axis used for note. 
# mxtics correspond to the smaller tick marks. 

gnuplot> set key x,y
gnuplot> set key box
set key defines what's being plotted and where it will appear!
and set key box defines a box around the key. 

gnuplot> set label "..." at x,y
will post a label at the point x,y. 

gnuplot> set grid

gnuplot> replot

gnuplot> f(x) = a*x + b
gnuplot> fit f(x) "data.dat" u 1:2 via a,b
is how we can perform curve fitting using GNUPlot. 
for fitting data sets from the 1st and 2nd columns of data.dat to a linear function f(x). 
the " ... via a,b " defines the variables which should be calculated. 

gnuplot> plot f(x), "data.dat" u 1:2
plots the estimated function f(x) and the original data points from data.dat in the same plot. 

gnuplot> set multiplot
is used to set an environment where we can plot multiple functions in the same window. 

to make life easier, we can write scripts which define the environment variables and load them so that we don't have to define them everytime we run GNUPlot. 
gnuplot> load environment.p

ohh, and yeah.
if you just wanted to use GNUPlot for plotting a basic function like sin(x) or cos(x), 
gnuplot> plot [.. : ..] [.. : ..] f(x) 
where [.. : ..] [.. : ..] set the x & y range in which the function f(x) is plotted.

=============================================================

any number of commands can appear on the same line as long as they're seperated by ; semicolons
strings can be setoff by either single or double quotes except for a few subtle differences - 
gnuplot> load "filename"
gnuplot>  cd 'dir'

\ is used break a a long command into multiple lines without effecting the output!

===================================
>>>to run iterations and to create animations! 
reset
set term gif animate
set output "animate.gif"
n=24 #n frames
dt=2*pi/n
set xrange [0:4*pi]
set yrange [-1:1]
do for [i=0:n]{
  plot sin(x+i*dt)/(1. + i/12.) w l lw 1.5 title sprintf("t=%i",i)
}
set output
===================================

Moving on, i also stumbled onto this awesome way to represent data in multi-rows/multi-columns.

That's all for now folks. 

Ohh yeah, one other thing. 

Yesterday was my birthday and almost everyone i met congratulated me on my birthday and then went on to ask me what i had done today, what special thing i had planned for later on in the night or what awesome thing i was going to do, it being my birthday and everything! 

I told them that i didn't need a reason to do something crazy or awesome or different. 
That i didn't need to wait for my birthday to do something that i wanted to. 
That my birthday was just another day in my endless journey towards being awesome... 

Popular posts from this blog

Farewell to Enthought

Arxiv author affiliations using Python

Elementary (particle physics), my dear Watson