Gnuplotting

Create scientific plots using gnuplot

September 23rd, 2016 | 4 Comments

Suppose you have a large circular container filled with sand and measure its density at different positions. Now the goal is to display your measurements as a heat map extrapolated from your measurements, but limiting that heat map to the inner part of the container as shown in Fig. 1.

circular heat map

Fig. 1 Sand density measured at different positions in a circular container (code to produce this figure, sand.pal, data)

The underlying measurements are provided in the following format:

# sand_density_orig.txt
#1      2        3        4      5       6
#prob   x        y        z      density description
"E01"   0.00000 -1.14161 -0.020  0.7500  "dense"
"E02"  -0.94493 -0.81804 -0.020  0.5753  "normal"
"E03"   0.75306 -0.72000 -0.020  0.7792  "dense"
...

Those data points have to be extrapolated onto a grid for the heat map, which can be achieved by the following commands.

set view map
set pm3d at b map
set dgrid3d 200,200,2
splot "sand_density1.txt" u 2:3:5

Fig. 2 shows the result which has two problems. The grid data is limited to the boundary given by the measurement points. In addition, the grid is always rectangular in size and not circular.

circular heat map

Fig. 2 Sand density measured at different positions in a circular container (code to produce this figure, sand.pal, data)

To overcome the first problem you have to add four additional points to the original data in order to stretch the grid boundary to the radius of the container. For that you have to come up with some reasonable extrapolation from the existing points. I did this in a very simple way by a mixture of linear interpolation or using the value of the nearest point. If you want to do the same with your data set you should maybe spent a little bit more effort on this.

# sand_density.txt
#1      2        3        4      5       6
#prob   x        y        z      density description
"E01"   0.00000 -1.14161 -0.020  0.7500  "dense"
...
"xmin" -1.50000  0.00000 -0.050  0.5508  "dummy"
"xmax"  1.50000  0.00000 -0.050  0.6634  "dummy"
"ymin"  0.00000 -1.50000 -0.050  0.7500  "dummy"
"ymax"  0.00000  1.50000 -0.050  0.6315  "dummy"

If you plot those modified data set you will get Fig. 3.

circular heat map

Fig. 3 Sand density measured at different positions in a circular container (code to produce this figure, sand.pal, data)

In order to limit the heat map to a circle you first extrapolate the grid using dgrid3d and store the data in a new file.

set table "tmp.txt"
set dgrid3d 200,200,2
splot "sand_density2.txt" u 2:3:5
unset table

Afterwards a function is defined in order to limit the points to the inner of the circle and plot the data from the temporary file.

circle(x,y,z) = sqrt(x**2+y**2)>r ? NaN : z
plot "tmp.txt" u 1:2:(circle($1,$2,$3)) w image

Finally a few labels and the original measurement points are added. The manually added points like xmin are removed by a smaller radius value. The result is then the nice circular heat map in Fig. 1.

r = 1.49 # make radius smaller to exclude interpolated edge points
set label 'normal' at -1,0.2 center front tc ls 1
set label 'dense' at 0.5,0.75 center front tc ls 1
set label 'very dense' at 0.3,-0.3 center front tc ls 1
plot "sand_density.txt" \
         u (circle($2,$3,$2)):(circle($2,$3,$3)) w p ls 1

June 21st, 2013 | 7 Comments

Sometimes a classical heat map will not be the best way to visualize your data in a two dimensional plane. This is especially the case, when only a few data points on the plane have different values. For example in Fig. 1 you find a projection from one vector to another to visualize its similarity. This is a method used in normal mode analysis of molecules to determine if two different
calculations yield similar results. As you can see only the data points near the diagonal vary, which is hard to see because of the small size of the points. In addition, points farer away from the diagonal having a small percentage value are more or less invisible – compare to Fig. 2.

Sparse data with image plot style

Fig. 1 Vector dot product expressed in percentage plotted with the image style (code to produce this figure, data)

In order to emphasize the data, we abounded the image plot style and use transparent circles as plotting style for visualizing the data as shown in Fig. 2.

Sparse data with circles plot style

Fig. 2 Vector dot product expressed in percentage plotted with the circles style (code to produce this figure, data)

In order to do so, we remove all values from the plot that are 0, by setting them to 1/0. Further we set the transparency of the circles to 20%. Before plotting the data we are sorting them regarding their percentage value in order to plot the highest values above the lower ones.

f(x) = x>2 ? x : 1/0
set style fill transparent solid 0.8 noborder
plot '<sort -g -k3 vector_projection.txt' u 1:2:(1):(f($3)) w circles lc palette

June 5th, 2013 | 7 Comments

If you are looking for nice color maps which are especially prepared to work with cartographic like plots you should have a look at colorbrewer2.org. On that site hosted by Cynthia Brewer you can pick from a large set of well balanced color maps. The maps are ordered regarding their usage. Figure 1 shows example color maps for three different use cases.

Colorbrew color maps

Fig. 1 Examples of color maps from colorbrewer2.org ordered in three categories (code to produce this figure, data)

The diverging color maps are for data with extremes at both points of a neutral value, for example like the below and above sea level. The sequential color maps are for data ordered from one point to another and the qualitative color maps are for categorically-grouped data with now explicit ordering.
Thanks to Anna Schneider there is an easy way to include them (at least the ones with eight colors each) into gnuplot. Just go to her gnuplot-colorbrewer github site and download the color maps. Place them in the same path as your plotting file, or add the three pathes of the repository to your load pathes, for example by adding the following to your .gnuplot file.

set loadpath '~/git/gnuplot-colorbrewer/diverging' \
    '~/git/gnuplot-colorbrewer/qualitative' \
    '~/git/gnuplot-colorbrewer/sequential'
YlGnBu color map from colorbrewer

Fig. 2 Photoluminescence yield plotted with the YlGnBu color map from colorbrewer2.org (code to produce this figure, data)

After this you can pick the right color map for you on colorbrewer2.org, keep its name and load it before your plot command. For example in Fig. 2 we are plotting again the photoluminescence yield with the sequential color map named YlGnBu. First we load the color map, then switch the two poles of the color map by setting the palette to negative, and finally plotting the data.

load 'YlGnBu.plt'
set palette negative
plot 'matlab_colormap.txt' u ($1/3.0):($2/3.0):($3/1000.0) matrix with image
Paired color map from colorbrewer

Fig. 3 Eight lines plotted with the Paired color map from colorbrewer2.org (code to produce this figure)

The nice thing of the palettes coming with gnuplot-colorbrewer is that they also include the corresponding line colors. In Fig. 3 you see the Paired qualitative color map in action with lines.

load 'Paired.plt'
plot for [ii=1:8] f(x,ii) ls ii lw 2

May 21st, 2013 | 1 Comment

As you may have noted, gnuplot and Matlab have different default color maps. Designing such a default map is not easy, because they should handle a lot of different things (Moreland, 2009):
– The map yields images that are aesthetically pleasing
– The map has a maximal perceptual resolution
– Interference with the shading of 3D surfaces is minimal
– The map is not sensitive to vision deficiencies
– The order of the colors should be intuitively the same for all people
– The perceptual interpolation matches the underlying scalars of the map

In his paper Moreland developed a new default color map that was mentioned already in a user comment. In Fig. 1 the map is used to replot the photoluminescence yield plotted in an earlier entry.

Default color map after Moreland, 2009

Fig. 1 Photoluminescence yield plotted with the default color map after Moreland, 2009 (code to produce this figure, data)

To use the default color map proposed by Moreland, just download default.plt and store it to a path, that is available to gnuplot. For example store it under /home/username/.gnuplotting/default.plt and add the following line to your .gnuplot file.

set loadpath "/home/username/.gnuplotting"

After that you can just load the palette before your plot command via

load 'default.plt'
Default gnuplot color palette

Fig. 2 Photoluminescence yield plotted with gnuplots default color palette (code to produce this figure, data)

In Fig. 2 the same plot is shown using the default color map from gnuplot, which is a little bit dark in my opinion.

Default Matlab color palette

Fig. 3 Photoluminescence yield plotted with Matlabs default color palette (code to produce this figure, data)

Figure 3 shows the jet color map from Matlab, which is a classical rainbow map with all its pros and cons.

March 12th, 2013 | 12 Comments

We discussed already the plotting of heat maps at more than one occasions. Here we will add the possibility to interpolate the data in a heat map figure.

Heat map

Fig. 1 A simple heat map (code to produce this figure, data)

Suppose we have the following data matrix, stored in heat_map_data.txt.

6 5 4 3 1 0
3 2 2 0 0 1
0 0 0 0 1 0
0 0 0 0 2 3
0 0 1 2 4 3
0 1 2 3 4 5

The normal way of plotting them would be with

plot 'heat_map_data.txt' matrix with image

But to be able to interpolate the data we have to use splot and pm3d instead.

set pm3d map
splot 'heat_map_data.txt' matrix

In Fig. 1 the result of plotting the data just with splot, without interpolation is shown. Note, that the result differs already from the plot command. The plot command would have created six points, whereas the splot command comes up with only five different regions for every axis.

Interpolated heat map

Fig. 2 A heat map interpolated to use twice as much points on every axis (code to produce this figure, data)

Now if we want to double the number of visible points, we can tell pm3d easily to interpolate the data by the interpolate command.

set pm3d interpolate 2,2

The two numbers 2,2 are the number of additional points along the x- and y-axis.
The resulting plot can be found in Fig. 2.

Strong interpolated heat map

Fig. 3 A heat map interpolated with an optimal number of points (code to produce this figure, data)

In addition to explicitly setting the number of points we can tell gnuplot to choose the correct number of interpolation points by itself, by setting them to 0.

set pm3d interpolate 0,0

Now gnuplot decides by itself how to interpolate, which leads to the result in Fig. 3.

March 15th, 2012 | 8 Comments

Suppose you have an image and wanted to add some lines, arrows, a scale or whatever to it. Of course you can do this easily with Gnuplot as you can see in Fig. 1.

jpg image

Fig. 1 Plotting a jpg image within your graph and adding a scale (code to produce this figure, image data). Image source: © SFTEP.

To plot the jpg image of the longnose hawkfish you have to tell the plot command that you have binary data, the filetype, and choose rgbimage as a plotting style. Also we ensure that the image axes are in the right relation to each other by setting ratio to -1.

set size ratio -1
plot 'fish.jpg' binary filetype=jpg with rgbimage

The scale needs a little more work, because Gnuplot can not plot a axis with tics to both directions of it. Hence we are using a bunch of arrows to achieve the same result. The text is than set by labels to the axis.

set arrow from 31,40 to 495,40 nohead front ls 1
set for [ii=0:11] arrow from 31+ii*40,35 to 31+ii*40,45 nohead \
   front ls 1
# set number and unit as different labels in order
# to get a smaller distance between them
set label '0'  at  25,57 front tc ls 1
set label 'cm' at  37,57 front tc ls 1
set label '5'  at 225,57 front tc ls 1
set label 'cm' at 237,57 front tc ls 1
set label '10' at 420,57 front tc ls 1
set label 'cm' at 442,57 front tc ls 1

September 26th, 2011 | 8 Comments

If you have not only some data points or a line to plot but a whole matrix, you could plot its values using different colors as shown in the example plot in Fig. 1. Here a 2D slice of the 3D modulation transfer function of a digital breast tomosynthesis system is presented, thanks to Nicholas Marshall from UZ Gasthuisberg (Leuven) for sharing the data.

Color map

Fig. 1 A simple color map (code to produce this figure, data)

All we need to create such a plot is the image plot style, and of course the data have to be in a proper format. Suppose the following matrix which represents z-values of a measurement.

0 1 2 3 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 3 2 1 0

In order to plot these values in different gray color tones, we specify the corresponding palette. In addition we apply the above mentioned image plot style and the matrix format option. The result is shown in Fig. 2.

set palette grey
plot 'color_map.dat' matrix with image
Color map

Fig. 2 A simple color map (code to produce this figure, data)

One remaining problem with Fig. 2 is, that the values on the x- and y-axis are probably not the one which you want, but the corresponding row and column numbers. One way to get the desired values is the use command, which can also be used with image. See Fig. 3 for the result.

plot 'color_map.dat' u (($1-4)/10):2:3 matrix w image
Color map

Fig. 3 A color map with a scaled x-axis (code to produce this figure, data)

Another way is to store the axes vectors together with the data. Therefore the data has to be stored as a binary matrix. The format of this matrix has to be the following:

<M>  <y1>   <y2>   <y3>   ... <yN>
<x1> <z1,1> <z1,2> <z1,3> ... <z1,N>
<x2> <z2,1> <z2,2> <z2,3> ... <z2,N>
 :      :      :      :   ...    :
<xM> <zM,1> <zM,2> <zM,3> ... <zM,N>

In Matlab/Octave the binary matrix can be stored using this m-file. The stored binary matrix can then be plotted by adding the binary indicator to the plot command.

plot 'color_map.bin' binary matrix with image

Note that in principle a color map can also be created by the splot command:

set pm3d map
splot 'data.dat' matrix

But if you create vector graphics with this command you will get a really big output file, because every single point will be drawn separately. For example check the graph from Fig. 1 as pdf created with plot and image and as pdf created with splot and pm3d map.