Trix & Graphix

Complex axis in Gnuplot

This post is to show the great flexibility of gnuplot regarding axis format. I'm going to explain some commands to get the next graph:

Yeah, I know, may be it's the most ugly and pointless plot ever. Nevertheless, it's a complex one, and it's not easy to get. That's the point ;-)

First of all, it's important to note that every plot has 4 different and independent axis called x, y, x2 and y2, respectively for bottom, left, top and right. Furthermore, each axis if the combination of three elements: the border (a straight line), the tics (short lines perpendicular to the border) and the tic labels (normally a number seting a value for the tic). The idea is that you can change every one of these elements separately.

Let's begin with the border. The border of the graph is just a square containing the plot. You can set or unset separately each one of the 4 borders, so it's easy to understand that you have 2x2x2x2=16 possibilities. To select what borders you want, gnuplot uses something like binary numaration. Each border has associated a power of two: 1, 2, 4 and 8 for bottom, left, top and right, respectively, like in the figure.

So for example if you want to set only the top border, you have to type:
set border 4
Other examples: if you want to set top and bottom borders, you have to sum both numbers, 4+1=5 and type set border 5. For the same reason, 0 sets no border, and 15 sets the four borders. Easy, isn't?

Now we are ready to go to tics. Tics and borders may be set or unset independently. So for example if you unset the top border, the y2 tics will still remain, so you can get unexpected results. To unset a tic set, you have to use the unset command, like for example
unset xtics
The same for ytics, x2tics and y2tics. By default, the 4 sets of tics are enabled.

Once you know in which axis you want to have tics, it's the moment to set how many you want. There are several ways of setting tics, and of course you can set the number of tics in each axis separately.

You can specify only the spacing between them, leaving the initial and final one to be set automatically by gnuplot by typing
set xtics 20
On the other hand, you can also specify the initial, final and the spacing by:
set xtics 20,1,30
Finally you can set the tics "by hand" following this syntax
set xtics ("one" 1, "two" 2, "3" 3, "{/Symbol p}" pi)
Where the string between quotes is the tic label to be set, and the number is the position of the label in the axis. This last options is really useful if you are dealing with a really complex and custimized plot.

A last note about tics and borders is that they all share the same line style. You can specify it like any other line options, like for example
set border 12 ls 2 lw 3 lc 3
This command will unset bottom and left boders. The rest will be, as well as the tics, dashed blue lines, 3 pixels width.

Let's now focus in the format of the tic labels. One important point to have into account is that if you want to unset only the labels, but keeping the tics, you can do it by changing the axis format to empty:
set format x ""
And of course analogously for the others 3 axis. By default x2 and y2 axis have this format. Regarding the formatting, you may set how numbers are displayed following C conventions, like for example
set x2tics "%02g"
and so on. There are plenty of places in internet where this convention is explained.

Other interesting options are offset and rotate. With these options you can shift the label of the tics and to rotate them, which is not an easy effect to get in other programs. A example of these is shown in the final example.

Finally, you can also change the color of the labels of the tics using the textcolor option, like
set x2tics textcolor lt 2

Well, I know, I have explained a lot of options, and not very much in detail. But rebember that all these options, and much more, are explained in the gnuplot interactive help. The idea of this post was to demonstrate to those who think gnuplot is lame that they are wrong.

Now I'll sumarize some of these concepts with the code to get of the initial example.

gnuplot << TOEND
# Setting the output file
set terminal postscript eps color enhanced "Helvetica" 20
set output 'borders.eps'

# Setting the top an right border (4+8=12).
#We change also the color and line style
set border 12 ls 2 lw 3 lc 3

# Setting the margins (distance between borders
#of the graph and the borders of the image)
set tmargin 5
set bmargin 5
set lmargin 5
set rmargin 15

# Setting the xtics
unset xtics
set x2tics ("-{/Symbol p}"-pi, -1, 0, "one" 1, 2, "three" 3, 4, 5, 6) textcolor
lt 2
set x2label "x2 axis"

# Setting the ytics
unset ytics
set y2tics -1,.2,1 textcolor lt -1 offset 2 rotate by 30
set format y2 "%.2f"

# The rest is just for a boring normal plot
set xrange [-5:5]
set yrange [-1:1]

set title "Pointless but rather complex graph"
plot sin(x) w l lt 1 lc rgb "#FF00FF" lw 6 t ''

convert -density 100 borders.eps borders.png

Uper to lowecase and vice versa

I'm going to tell three ways of doing the same task: changing the capitalization. This is a pretty comon task, and there are many ways of doing it.

The first way is to use my favourite text editor: Vim. The idea is to open the file to be modified. Then, select the whole content of the file and push u to change it to lowercase. Analogously push U to change it to upercase. To select the whole file, you just have to type ggVG. The explanation is as follows: the first gg is to go to the begining of the file, V is for changing to select lines mode, and G to go to the end of the file.

This last way is useful (and fast) if you have to change the content of just one file. It is nevertheless less useful if you have to change many files. In these cases, is better to use awk or sed.

To use awk (my favourite option) you only have to use the function tolower, like in this example:

echo TEXT TO BE CHANGED TO LOWECASE | awk '{print tolower($0)}'

and similary the function toupper changes the string to upercase.

This task may also be achieved with sed. To do it, the command line should be now

echo text to capitals | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'

Well, this is all for today. It looks like the sed way is less elegant. The fact is that I'm pretty sure there must be a better way, althought I don't know it yet ;-).

Simple animations with Imagemagick

Imagemagick allows to generate simple animations (this is, a gif file) by using several input images as frames in a movie. Here I'm going to show the main idea, which it's actually very simple.

In order to have several images to be mounted in the movie, I have used a bash script together with gnuplot. This will generate 20 slightly different images of a sinusoidal fuction, little bit shifted between them. This is the code:



# Note the -w option in seq
for i in $(seq -w 1 $N); do
gnuplot << TOEND
# Setting the output
set terminal postscript eps color "Helvetica" 20
set output 'sin$i.eps'

# This removes the numbers in the graph and sets the grid
set format x ""
set format y ""
set grid

# The plot itself
plot sin(x+2*pi/$N*$i) w l lt 1 lc 3 lw 4 t ''


# Now transform the postscript file into a png one, and remove the eps
convert -density 50 -layers flatten sin$i.eps sin$i.png
rm sin$i.eps

It's important to note the "-w" option in the seq command. This will generate numbers with the correct padding. This is, it will generate the list "01 02 ... 09 10 11 ..." instead of "1 2 ... 9 10 11 ..." which may be a problem when you want to list the resulting files in the correct order to create the movie.

So now we have 20 png files. To merge them, the convert command may be invoked with just an option to set the delay (in miliseconds) between images.

convert -delay 10 sin*png animation.gif

That's all, it's pretty easi, isn't?

Ant this is the result:

EDIT: By some unknown reason, gif animations doesn't seem to work when I upload them to Picasa. Well, you have to trust me, it evolves with time, and it's even pretty funny ;-).

ACTUALIZATION: Using other (my own) web server, I have been abled to upload a real gif animation. With minor changes in the script, this is what I get:

Greek letters in gnuplot

Gnuplot is a powerful scientific plotting program. In fact is more powerful than people usually think first time time they use it. May be this is because it has no GUI. Instead of that, you have to type commands in a plain boring terminal.

Well, in this post I'm going to put an example of how to perform a relatively complex graph, using greek letters and legend. Of course, everything I use here is explained perfectly in the help.

Gnuplot has several output formats (called terminals). Depending on what you are going to do later with the output you get from the program, you can use one or another (from raster formats like png of jpeg, up to vectorial ones such as eps or svg). In general I advice to use the terminal postscript, and later on to export the file to a raster format like png using a images manipulation program like Imagemagick. I also advice to use the options: eps (to get encapsulated postscript code), color (to have colors enabled) and enhanced (to be able to use advanced postscript options such as greek letters).

To get a greek letter, you have to use the postscript terminal, with the option enhanced. Then, to add the alpha greek letter, you have to type {/Symbol a}. Analogously for other letters, changing the "a" by other, like "D" for Delta and so on...

Finally, the thing I like most about Gnuplot is that it's easily scriptable. Here there is a simple script which shows all these ideas in practice (comments are added to make code more readable):


gnuplot << TOEND
# Setting the terminal postscript with the options
set terminal postscript eps color enhanced "Helvetica" 20

# Setting the output file name
set output 'plot.eps'

#Setting up the grid and labels
set grid
set title "A simple graphic without LaTeX"
set xrange [-pi:pi]
set xlabel "Angle {/Symbol a}"
set ylabel "{/Symbol Dw}"
set ytics 0.5
set xtics ("{/Symbol p}" -pi, "{/Symbol p}/2" -pi/2, "0" 0, "{/Symbol p}/2" pi/2, "{/Symbol p}" pi)

# Setting the legend
set key horizontal below height 2
set key box lt 2 lc -1 lw 3

# The plot itself (\ is to broke lines)
set size 1,1
plot sin(x)**2 w l lt 1 lc 1 lw 3 t "sin^2({/Symbol a})", \
sin(2*x)/x w l lt 2 lc 3 lw 3 t "sin(2{/Symbol a})/{/Symbol a}", \
sin(x) w l lt 3 lc 2 lw 3 t "sin{/Symbol a})"

convert -density 150 plot.eps plot.png
rm plot.eps

And this is the result (click to enlarge).

Well, is not bad, but it's not still wonderful. In a future post I will explain how to use the terminal epslatex to get impressive book-quality results ;-).

My own (botch) version of LaTeX in blogger

There are, as far as I know, at least two different ways of using within blogger. In both cases the idea is the same, and it's similar as in some forums or in the TeXIM plugin for AMSN: somewhere there is a third part server which uses a LaTeX distribution to generate a dvi file, together with some other tools to convert this file into an image, which can be included in the html code of your blog.

Well, the problem with such solutions is that they parasite a server developed for this purpose, but this server has few (or not at all) options. In particular, the image you get is only useful with light backgrounds, so you lose freedom for choosing the design of your blog. Just to put an example, this is what I get:



I have to say, first of all, that I have not found the perfect solution to this problem. Instead of that, my aproach consists in adding the image by hand, using Picasa. Yeah, I know, I know,... it isn't very satisfactory, but if you don't need thousands of equations, it could be good enought. Again, just for example, this is my result:

Is not bad, isn't it? But the main problem remains unsolved: how to get the image with the equation. Well, here is my solution, a bash script:



cat > equation.tex << PART1



cat equation.asc >> equation.tex

cat >> equation.tex << PART2

latex equation.tex
dvips -f -E -o equation.dvi
convert -density $DENSITY equation.png

rm equation.log equation.aux equation.tex equation.dvi

The idea is pretty simple. To use it, you have to type your formula in the file equation.asc, in standard latex (rebembering that you are inside a eqnarray environment). Then, by running the script in your computer you get a png file with the equation, which you can upload by hand. The good thing of this aproach is that you can choose the colour of the equation playing around the variable COLOR (setting the colour you want in rbg), as well as with the size of the image changing DENSITY.

A last thing is that in order to run successfully the script, a distribution and Imagemagick have to be installed and working in the system you want to run the script.

cat and tac

Unix-like system are based on a very simple set of ideas, one of them is: every program has to solve just one task, and do it right. Well, this is exactly what does one of the most common tools in Linux: cat.

Many people use cat just for getting the output from an ascii file, in commands like

$ cat file.asc
Well, open a file, decode the ascii format and print out a human-readable version in screen is a great thing, but it is not the only thing cat can do.

cat may be used to concatenate files, independently of their format. For example
$ cat file1.asc file2.asc > files1-2.asc
joins both files in another ascii format file.

Not only ascii files, but it also work with binary ones, for example with metheorological EXTRA format
$ cat file1.ext file2.ext > files1-2.ext
But the most powerful feature of cat is that it is a text editor. To use it, you have to type:
$ cat > file.txt << EDIT
some text

you want to add
into your file
which means something like "take the input of the keyboard and insert it in file.txt. Wait until I type again EDIT, which will mean I want you to stop listening".

This feature is extremely useful for scripting purposes, as you can make scripts which create other scripts and so on. May be I will put some example of this in a (I hope) near future.

Finally, there exists another command, which I found out few time ago, tac. This programs do exactly the same as cat, but from bottom to up. This is, tac may be used for printing a file reversing the order of rows. It may be very useful when you know that the interesting part is at the end of a file.

BTW. Many people use the command:
$ cat file.txt | grep something
trying to look for "something" in the file. There is no point to do that. grep is another tool which does not need at all cat to work. The correct form would be:
$ grep something file.txt

A new project: justification

Two years ago I started this blog. Well, actually I began it and later on I forgot it because I thought I had nothing interesting to say. Now the situation has not changed too much, but I have had a brand new idea*: I could convert this in something like a list of technical recipes about Unix commands and related issues, like computing, advanced scientific plotting and so on.

The thing is that sometimes I need to know how to do a technical task, like filtering some lines within an ascii file, or plot complex graphs with gnuplot, or I get an unknown error when compiling fortran programs,... in these cases I spend up to a whole day looking for in Internet how to solve the problem. From now on, every time I solve a problem, I will publish the solution here, so it may be helpful for something else, as well as for myself in future (I'm very chaotic).

One thing I should explain is why I write so poorly. And the answer is simple: I don't know English enough. You could ask me then why do you write in English? Well, I want to learn, and keep practising is a great opportunity to improve my grammar and increase my vocabulary. That's the only reason.

* I had the idea when I saw this great blog.