Monday, September 19, 2011

Colours and formats

It is a difficult task to control colours in GNUPLOT. Each type of output file (png, ps, eps, etc.) has its own colour definition. In order to know which colour belongs to each line style, an easy method is to type test after selecting the output format.

Example:
  • set term png
  • set output 'test_colour.png'
  • test

Then, the file test_colour.png will look like:

This can be done for all output formats (png, ps, eps...).

Friday, September 2, 2011

Palettes

There are several methods for defining a colour palette in GNUPLOT. Here one of them is presented and explained in a simple way. If you want to go deep into this topic, you will have to look for more extensive references (some of them listed as useful links).

First of all, we need to define the number of colours that our palette will contain using this command:
  • set palette maxcolors <number>

Then, what we have to do is to specify the colour of each part of the palette using the following command:
  • set palette defined (<position1> "<colour1>", <position2> "<colour2>", ...)

Example (A):
  • set palette maxcolors 100
  • set palette defined (0 "red", 99 "blue")
These lines will create a palette of 100 colours from red (position 0) to blue (position 99). Between both positions, GNUPLOT will perform a gradation.

We can specify as much colours as we want, for example (B):
  • set palette maxcolors 1000
  • set palette defined (\
  • 0 "red", \
  • 249 "yellow", \
  • 749 "green", \ 
  • 999 "blue")

As a final example, if we want to ascribe a specific colour to the middle value of the palette (in order, for instance, to separate positive and negative values), we will have to write something like this (C):
  • set palette maxcolors 4001
  • set palette defined (\
  • 0 "red", \
  • 1000 "orange", \
  • 1600 "dark-yellow", \
  • 1999 "yellow", \
  • 2000 "grey", \
  • 2001 "green", \
  • 2400 "dark-green", \
  • 3000 "cyan", \
  • 4000 "blue")
Note that only the value in the middle corresponds to gray. The previous and the following values (with a precision that depends on the number of colours selected) will correspond to yellow and green respectively.

Caution: The palettes presented here work in eps output files. If you work with other types of file, you may have some troubles with the colours.

Thursday, September 1, 2011

Converting eps to png

Although this post is not dedicated to GNUPLOT, it can be useful for those working with image files. If you have tried to use the Linux command convert with eps files, you will have found that it usually ends in a dramatic reduction in the resolution of your figures, i.e. the next command does not work properly:
  • convert whatever.eps whatever.png
This is because the eps files, as they are vectorial, do not have a concrete resolution and the one used by default in the conversion is very rude. Nevertheless, adding the option density you can specify the "density" of the resulting png image in points per inch (I do not really understand what represents; if you do, please leave a comment). The syntaxis is, then:
  • convert -density <density> whatever.eps whatever.png

Example:
  • convert -density 200 whatever.eps whatever.png

Tuesday, July 12, 2011

Joining string variables

When defining file names or titles, is often useful to join string variables in order to make our scripts more comprehensible. To do so, you have to join this variables using dots.
Example:
  • DATA='satellite'
  • DATE='12/07/12'
  • EXTENSION='dat'
  • set title DATA.' measurements - '.DATE
  • plot DATA.'_data.'.EXTENSION
This will plot the file "satellite_data.dat" and will entitle the plot as "satellite measurements - 12/07/12"

Monday, July 11, 2011

Rotating axes labels

The command to rotate the labels in one of the axis is:
  • set <axis>tic rotate by <angle> scale <size>
The parameter scale must be a number and accounts for the size of the tics in the selected axis.
Example:
  • set xtic rotate by 90 scale 0.5
Caution: Be careful when rotating an angle non multiple of 90 because it can lead to non desired effects.

Wednesday, July 6, 2011

Conditional statements

The syntaxis for writing conditional statements in GNUPLOT is:
  • if (<condition>) <command1>; <command2>; ...; else <command1>; <command2>; ...

List of conditions:
  eq  equal to (for strings)
  ne  non equal to (for strings)
  ==  equal to (for numbers)
  !=  non equal to (for numbers)
  <   lower than
  <=  lower than or equal to
  >   greater than
  >=  greater than or equal to

Combining several conditions:
  &&  and
  ||  or

Example:
  • if (FILE_NUM==1) \
  • 'whatever1.dat' u 1:2 w l lw 1 lt 3 notitle;\
  • 'whatever1.dat' u 1:3 w l lw 5 lt 1 notitle
  • if (FILE_NUM==2) \
  • 'whatever2.dat' u 1:2 w l lw 1 lt 3 notitle;\
  • 'whatever2.dat' u 1:3 w l lw 5 lt 1 notitle

Tuesday, July 5, 2011

Working with the columns of the data file

This post talks about how to control the information about the columns in a data file.

1) Once we want to plot the data contained in a data file, we can just write:
  • plot 'whatever.dat'
This will take the first column of the data file as the X coordinate and the second column as the Y coordinate.

2) In order to specify which column corresponds to each coordinate, we can write, for instance:
  • plot 'whatever.dat' u 3:2
It will take the data in the third column as the X coordinate and the data in the second column as the Y coordinate.

3) We can also treat any of the columns as a variable (previously defined):
  • COL=2
  • plot 'whatever.dat' u 3:COL

4) In order to perform mathematic operations to the number of any column, we must use parentheses:
  • COL=3
  • plot 'whatever.dat' u COL:(COL-1)

5) Nevertheless, if we want to operate the values contained in the column (not the number of the column), we have to use the dollar sign:
  • plot 'whatever.dat' u (cos($3)):5

6) Finally, if we want to perform the mathematic operation to the value of a column specified with a variable, we will have to use the function column:
  • COL=3
  • plot 'whatever.dat' u (cos(column(COL))):5

Wednesday, June 29, 2011

Backslash at the end of a line

This tool is not essential for programming, but it can make our scripts more legible and easy to interpret. It is useful when you have to put several commands in the same script line but you preffer to separate them while working with your text editor. Then, the line:
  • plot 0 w l ls 0 notitle, 'whatever1.dat' u 1:2 w l lw 1 lt 3 notitle, 'whatever1.dat' u 1:3 w l lw 5 lt 1 notitle, 'whatever2.dat' u 1:2 w l lw 1 lt 3 notitle, 'whatever2.dat' u 1:3 w l lw 5 lt 1 notitle
is equivalent to:
  • plot \
  • 0 w l ls 0 notitle, \
  • 'whatever1.dat' u 1:2 w l lw 1 lt 3 notitle, \
  • 'whatever1.dat' u 1:3 w l lw 5 lt 1 notitle, \
  • 'whatever2.dat' u 1:2 w l lw 1 lt 3 notitle, \
  • 'whatever2.dat' u 1:3 w l lw 5 lt 1 notitle

This is not restricted to the plot command, but you can use it wherever you want. So:
  • set palette defined (0 "red", 1000 "orange", 1600 "dark-yellow", 1999 "yellow", 2000 "grey", 2001 "green", 2400 "dark-green", 3000 "cyan", 4000 "blue")
is again equivalent to:
  • set palette defined (\
  • 0 "red", \
  • 1000 "orange", \
  • 1600 "dark-yellow", \
  • 1999 "yellow", \
  • 2000 "grey", \
  • 2001 "green", \
  • 2400 "dark-green", \
  • 3000 "cyan", \
  • 4000 "blue")

Caution: Leaving a blank space after the backslash will result in a failure of the script.

    Welcome

    • Have you ever been stuck when trying to plot some sort of data in a not standard way?
    • Have you felt lonely because software just do not understand what you are trying to tell it?
    • Are you unable tu understand why the Google searches do no solve your problems?
    I am here to help you. I will try to post some easy (but absolutely hard to find) tricks when plotting data with GNUPLOT and other similar pieces of software.