Gnuplotting

Create scientific plots using gnuplot

March 2nd, 2015 | 4 Comments

If you are a regular gnuplot user you most probably want to reuse some common settings. I normally avoid it on this blog to have easy scripts that run as standalone files, but during my work I use a lot of small config files.

Bessel functions

Fig. 1 Bessel functions from order zero up to six plotted with the dark2 line colors. (code to produce this figure, dark2.pal, xyborder.cfg, grid.cfg, mathematics.cfg)

Let us start with the Bessel function example from the last blog entry. As you can see in Fig. 1, it is a 2D plot, including axes, a grid, line colors, and definitions of higher order Bessel functions. All of those could be easily stored in small config files and reused in other plots.
As an example I will start with the axes. Here, I have four different config files, called xyborder.cfg, xborder, yborder.cfg, noborder.cfg, which do exactly what their names would suggest. Here are the first and last file:

# xyborder.cfg
set style line 101 lc rgb '#808080' lt 1 lw 1
set border 3 front ls 101
set tics nomirror out scale 0.75
set format '%g'
# noborder.cfg
set border 0
set style line 101 lc rgb '#808080' lt 1 lw 1
unset xlabel
unset ylabel
set format x ''
set format y ''
set tics scale 0

In the main plotting file I then just have to load the setting I like to have and I’m done. The same can be done for adding a grid, the right line color definitions and the extra Bessel functions leading to the following excerpt from the main plotting file:

# set path of config snippets
set loadpath './config'
# load config snippets
load 'dark2.pal'
load 'xyborder.cfg'
load 'grid.cfg'
load 'mathematics.cfg'

The set loadpath command tells gnuplot the directory where it can find all the configuration snippets. If you want to see an overview, look at my gnuplot configuration snippets and at the collection of palettes and line colors.

If you want to include more complicated settings, you have to use the macro setting of gnuplot. Fig. 2 is a reproduction of an earlier entry plotting a vector field with arrows. It included an lenghty definition of how to plot these arrows. If you want to do it several time and define the arrows in the same way every time you should also put it into a config file, this time as a variable (macro). In our example it looks like

color_arrows = 'u ($1-dx($1,$2)/2.0):($2-dy($1,$2)/2.0):(dx($1,$2)):(dy($1,$2)):\
(v($1,$2)) with vectors head size 0.08,20,60 filled lc palette'

In the main file the only thing we have then to do is

set macros
load 'noborder.cfg'
load 'moreland.pal'
load 'arrows.cfg'

# [...] 

plot '++' @color_arrows

Important is the first line that enables the use of macros in gnuplot which is disabled by default.

4 Comments

  1. I love your pot with six Bessel functions. My “gnuplot” only find the one with 0 order and the one with order 1. How do I plot the higher order Bessel functions?

    Thanks.

  2. hagen says:

    You can use the following recursion relationship:

    # Bessel functions (after Bronstein 2001, eq. 9.54a)
    besj2(x) = 2*1/x * besj1(x) - besj0(x)
    besj3(x) = 2*2/x * besj2(x) - besj1(x)
    

    See also bessel.gnu.

  3. Pawe? says:

    Hi,
    I am a Debian user and I have a problem with the wxt terminal. I’ve searched the Internet and found something. Debian has reportedly stopped the support for something necessary for the wxt terminal to work. I can view the plot with x11 terminal but the result is far from satisfactory. Do you know of any solution for this problem?

    Thanks for a great page.

  4. hagen says:

    Hi,
    I know this problem, but haven’t spend time so far on finding a solution. As a workaround I’m using directly the pngcairo terminal.

Leave a Reply