Skip to main content

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 

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

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 [... : ...]
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! 
set term gif animate
set output "animate.gif"
n=24 #n frames
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

Animation using GNUPlot

Animation using GNUPlotI've been trying to create an animation depicting a quasar spectrum moving across the 5 SDSS pass bands with respect to redshift. It is important to visualise what emission lines are moving in and out of bands to be able to understand the color-redshift plots and the changes in it.
I've tried doing this using the animate function in matplotlib, python but i wasn't able to make it work - meaning i worked on it for a couple of days and then i gave up, not having found solutions for my problems on the internet.
And then i came across this site, where the gunn-peterson trough and the lyman alpha forest have been depicted - in a beautiful manner. And this got me interested in using js and d3 to do the animations and make it dynamic - using sliders etc.
In the meanwhile, i thought i'd look up and see if there was a way to create animations in gnuplot and whoopdedoo, what do i find but nirvana!

In the image, you see 5 static curves and one dynam…

on MOOCs.

For those of you who don't know, MOOC stands for Massively Open Online Course.

The internet is an awesome thing. It's making education free for all. Well, mostly free. But it's surprising at the width and depth of courses being offered online. And it looks like they are also having an impact on students, especially those from universities that are not top ranked. Students in all parts of the world can now get a first class education experience, thanks to courses offered by Stanford, MIT, Caltech, etc.

I'm talking about MOOCs because one of my new year resolutions is to take online courses, atleast 2 per semester (6 months). And I've chosen the following two courses on edX - Analyzing Big Data with Microsoft R Server and Data Science Essentials for now. I looked at courses on Coursera but I couldn't find any which was worthy and free. There are a lot more MOOC providers out there but let's start here. And I feel like the two courses are relevant to where I …

On programmers.

I just watched this brilliant keynote today. It's a commentary on Programmers and the software development industry/ecosystem as a whole.

I am not going to give you a tl;dr version of the talk because it is a talk that I believe everyone should watch, that everyone should learn from. Instead, I am going to give my own parallel-ish views on programmers and programming.
As pointed out in the talk, there are mythical creatures in the software development industry who are revered as gods. Guido Van Rossum, the creator of Python, was given the title Benevolent Dictator For Life (BDFL). People flock around the creators of popular languages or libraries. They are god-like to most programmers and are treated like gods. By which, I mean to say, we assume they don't have flaws. That they are infallible. That they are perfect.
And alongside this belief in the infallibility of these Gods, we believe that they were born programmers. That programming is something that people are born wit…