Gnuplotting

Create scientific plots using gnuplot

September 29th, 2014 | 6 Comments

Some time ago I introduced already a waterfall plot, which I named a pseudo-3D-plot. In the meantime, I have been asked several times for a colored version of such a plot. In this post we will revisit the waterfall plot and add some color to it.

Colored waterfall plot

Fig. 1 Waterfall plot of head related impulse responses. (code to produce this figure, color palette, data)

In Fig. 1 the same head related impulse responses we animated already are displayed in a slightly different way. They describe the transmission of sound from a source to a receiver placed in the ear canal dependent on the position of the source. Here, we show the responses for all incident angles of the sound at once. At 0° the source was placed at the same side of the head as the receiver.

The color is added by applying the Moreland color palette, which we discussed earlier. The palette is defined in an extra file and loaded, this enables easy reuse of defined palettes. In the plotting command the palette is enabled with the lc palette command, that tells gnuplot to use the palette as line color depending on the value of the third column, which is given by color(angle).

load 'moreland.pal'
set style fill solid 0.0 border
limit360(x) = x180?360-x:x
amplitude_scaling = 200
plot for [angle=360:0:-2] 'head_related_impulse_responses.txt' \
    u 1:(amplitude_scaling*column(limit360(angle)+1)+angle):(color(angle)) \
    w filledcu y1=-360 lc palette lw 0.5

To achieve the waterfall plot, we start with the largest angle of 360° and loop through all angles until we reach 0°. The column command gives us the corresponding column the data is stored in the data file, amplitude_scaling modifies the amplitude of the single responses, and +angle shifts the data of the single responses along the y-axis to achieve the waterfall.

Even though the changing color in the waterfall plot looks nice you should always think if it really adds some additional information to the plot. If not, a single color should be used. In the following the same plot is repeated, but only with black lines and different angle resolutions which also have a big influence on the final appearance of the plot.

Colored waterfall plot

Fig. 2 Waterfall plot of head related impulse responses with a resolution of 5°. (code to produce this figure, data)

Colored waterfall plot

Fig. 3 Waterfall plot of head related impulse responses with a resolution of 2°. (code to produce this figure, data)

Colored waterfall plot

Fig. 4 Waterfall plot of head related impulse responses with a resolution of 1°. (code to produce this figure, data)

April 2nd, 2012 | 9 Comments

Since last month the new Gnuplot version 4.6 is officially available. There are a lot of interesting changes in this new version and we will cover the bigger ones within the next posts. Here we start with, in my opinion, the nicest new feature: block-structured conditions and loops.

Until 4.6 an iteration over different lines of code was only possible with the help of an extra file. This technique was used, for example, for the gif animation entry. There the loop was executed by

t = 0
end_time = 1
load 'bessel.plt'

with the file bessel.plt containing the code to execute within the loop

# bessel.plt
t = t + 0.02
outfile = sprintf('animation/bessel%03.0f.png',50*t)
set output outfile
splot u*sin(v),u*cos(v),bessel(u,t) w pm3d ls 1
if(t<end_time) reread;

This can now be reformulated in a much shorter way by applying the new do command and the block-structure given by the { }

do for [t=0:50] {
    outfile = sprintf('animation/bessel%03.0f.png',t)
    set output outfile
    splot u*sin(v),u*cos(v),bessel(u,t/50.0) w pm3d ls 1
}

Now there is no need for an additional file. The only thing to consider is the change of the index t, because for the for-loop t has to be an integer.

The block-structure can in the same way be applied to the if-statement.

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

November 29th, 2011 | 3 Comments

A spectrogram is a time-frequency representation that shows how the spectral content of a signal varies with time. In Fig. 1 the spectrogram of the German sentence “Achte auf die Autos” is shown.

Spectrogram

Fig. 1 Spectrogram plotted with plot (code to produce this figure, data)

The spectrogram is plotted as mentioned in the color maps entry.

plot 'spec.dat' binary matrix with image

In addition the letters were putted on the graph at their corresponding time of occurrence. The letters itself and their positions are stored in the two lists syl and xpos. In order to access the single entries of these lists within a for loop the function word is needed.

# Adding the syllables
syl  = 'A    ch   te   a    u    f    d    ie   A    u    t    \
o    s   '
xpos = '0.15 0.22 0.36 0.44 0.49 0.56 0.62 0.66 0.89 1.01 1.16 \
1.26 1.42'
set for [n=1:words(syl)] label word(syl,n) at word(xpos,n),6800

Another way to plot the spectrogram is by using splot which will result in a different kind of smoothing algorithm of the spectrogram, as can be seen in Fig. 2.

Spectrogram

Fig. 2 Spectrogram plotted with splot (code to produce this figure, data)

But to get this result we have to fix some of the margins, because plot is two-dimensinal and splot is three-dimensional which is not desired here.

set border 10 front ls 11
set tmargin at screen 0.75
set bmargin at screen 0.25
set rmargin at screen 0.95
set lmargin at screen 0.15

October 10th, 2011 | No Comments

In the last entry we have seen how to use a color map to represent matrix data. Another way to visualize such kind of data is to code their values not as color, but as height information using so called pseudo 3D plots.

Pseudo 3D plot

Fig. 1 Pseudo 3D plot of basilar membrane activity (code to produce this figure, data)

Suppose we have some data like spectra with different parameters, slightly shifted and plotted into the same figure, or different oscillations over time as shown in Fig. 1. There, the movement of the basilar membrane to an input stimuli dependent on the center frequency in ERB is plotted over time. The movement on the basilar membrane is dependent on the frequency of the incoming stimulus, with different frequencies acting on different places along the membrane. In order to plot this kind of data the for command of Gnuplot can be used to iterate through the data. The pseudo 3D effect is realized by shifting the data in every iteration one ERB by the +ii part and the usage of filledcurves to overwrite not visible parts of the plot with white color.

set style fill solid 1.0 border rgb 'black'
plot for [ii=25:1:-1] 'bmm.txt' u (f(column(ii))+ii) \
    w filledcu y1=-2 ls 1

The amplitude of the data was originally stored in order to fit in a plot given in Hz. Hence, we have to convert the data into ERB. This is done by the function f. As arguments to the function the values of each column are given in the iteration. Therefore, the column number is indexed by the column function.

June 24th, 2011 | No Comments

The filledcurves style is only available for 2d plots. But it can be used with some limitations with splot in 3d plots as well. In this entry we want to visualize an effect known from psychoacoustics, called comodulation masking release. The effect describes the possibility of our hearing system to perceive a masked tone (in this case at 700 Hz) easier in the presence of so called comodulated maskers present in other auditory filters. Comodulation describes the fact, that all maskers have the same envelope, as can be seen in Fig. 1.

CMR

Fig. 1 Visualization of the comodulation masking release using splot and filledcurves (code to produce this figure, gfb_loop.gnu, gfb.dat, sig.dat, noise.dat)

First we start with the gammatone filters. The values for them are stored in the gfb.dat file as one column per filter. In order to apply different colors to different filters the style function sty(x) is defined. The data(x) function is defined to be able to plot the filters in a particular order. This will result in the nice effect of overlapping filters shown in Fig. 2.

sty(x) = x<7 ? 1 : x<10 ? 2 : x<12 ? 1 : x==12 ? 3 : x<15 ? 1 : \
    x==15 ? 2 : 1
data(x) = x<12 ? x : 29-x

The filter bank itself is plotted by the gfb_loop.gnu function. There the data are plotted first as filledcurves and then as a line. This two step mechanism has to be used, because the filledcurves style is not able to draw an extra line in 3d. Hence it has to be done in the extra gfb_loop.gnu function, because the simple for iteration only works for a single plot and is not able to plot the line around the filters.

# gfb_loop.gnu
splot 'gfb.dat' u 2:1:(column(data(i))) w filledcurves \
    ls sty(data(i)), \
      ''        u 2:1:(column(data(i))) ls 4
i = i+1
if (i<maxi) reread
CMR

Fig. 2 Plotting gammatone filters with an extra loop file (code to produce this figure, gfb_loop.gnu, gfb.dat)

Thereafter the modulated noise and its envelope and the signal are plotted in different parts of the graph by explicitly giving the x position.
The result is shown in Fig. 1.

splot 'noise.dat' u  (300):1:2 ls 11, \
      ''          u  (300):1:3 ls 14, \
      ''          u  (400):1:2 ls 12, \
      ''          u  (400):1:3 ls 14, \
      ''          u  (700):1:2 ls 13, \
      ''          u  (700):1:3 ls 14, \
      ''          u (1000):1:2 ls 12, \
      ''          u (1000):1:3 ls 14, \
      ''          u (1100):1:2 ls 11, \
      ''          u (1100):1:3 ls 14
splot 'sig.dat'   u  (700):1:2 ls 14

May 6th, 2011 | 2 Comments

loudspeaker circle

Fig. 1 A circular loudspeaker array drawn with the object command (code to produce this figure, set_loudspeaker function)

In one of the last entries we have seen how to plot a loudspeaker with Gnuplot.
This time we will have a look at the case of setting more than one loudspeaker to your plot. Furthermore we allow the placement of the loudspeakers after entries in a data file.
Let us assume we have a data file containing the x position, y position and orientation phi of a single loudspeakers per line. Now we have to read the data with Gnuplot and set the objects according to the data. This can be done by a dummy plot, because by applying the plot command, variables can be stored. For the dummy plot we setting the output of the plot command to table and use /dev/null as the place to write the data.

# --- Read loudspeaker placement from data file
set table '/dev/null'
add_loudspeaker(x,y,phi) = sprintf(\
    'call "set_loudspeaker.gnu" "%f" "%f" "%f" "%f";',x,y,phi,0.2)
CMD = ''
plot 'loudspeaker_pos.dat' u 1:(CMD = CMD.add_loudspeaker($1,$2,$3))
eval(CMD)
unset table

The plot command now enables us to add the data from the file to the variable CMD, which is then executed by the eval command. To create the variable, the add_loudspeaker function creates a string with the data for every single line of the data file. The eval(CMD) calls the set_loudspeaker.gnu function once for every single data line, which corresponds to a single loudspeaker. The set_loudspeaker.gnu function itself does the same as we have done in the draw a single loudspeaker entry, but in addition it uses a rotation matrix to change the orientation of the single loudspeakers.

After having set the loudspeakers, we add some activity to three of the loudspeakers and finally get the result in Fig. 1.

# --- Plot loudspeaker activity
set parametric
fx(t,r,phi) = -1.5*cos(phi)+r*cos(t)
fy(t,r,phi) = -1.5*sin(phi)+r*sin(t)
set multiplot
set trange [-pi/6+pi/8:pi/6+pi/8]
plot for [n=1:3] fx(t,n*0.25,pi/8),fy(t,n*0.25,pi/8) w l ls 2
unset object
set trange [-pi/6-pi/8:pi/6-pi/8]
plot for [n=1:3] fx(t,n*0.25,-pi/8),fy(t,n*0.25,-pi/8) w l ls 2
set trange [-pi/6:pi/6]
plot for [n=1:3] fx(t,n*0.25,0),fy(t,n*0.25,0) w l ls 1
unset multiplot

The three waves before the desired loudspeakers are plotted within an iteration that effects the radius by using the for command. The unset object is executed after the first plot in the multiplot environment, because the loudspeakers should only be drawn once.