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.
#!/bin/bash

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 ''
TOEND

convert -density 100 borders.eps borders.png

3 Comments:

piponazo said...

Thank you for sharing this useful information. ;)

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Ho do you plot the wavelength on the lower x-axis and the energy on the upper x-axis?