Gnuplotting

Create scientific plots using gnuplot

September 29th, 2014 | 4 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) = x<1?x+360:x
color(x) = x>180?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 19th, 2011 | 3 Comments

Often we use a specific line color or output terminal in Gnuplot. Or we are not satisfied with one of the default settings, for example the font used by the png terminal. There exists an easy way to fix these settings. Gnuplot reads a startup file, called .gnuplot under Linux and GNUPLOT.INI under Windows. First it searches the current directory and then your home directory after that file.

Here is an example of what is possible to include in the startup file.

# enable macros
set macros
# search for functions or data files in these directories
set loadpath '/usr/local/lib/gnuplot'
# add default line colors
set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 5   # blue
set style line 2 lc rgb '#dd181f' lt 1 lw 2 pt 7   # red
# add macros to select the desired line style
BLUE = "1"
RED = "2"
# add macros to select a desired terminal
WXT = "set terminal wxt size 350,262 enhanced font 'Verdana,10' \
   persist"
PNG = "set terminal pngcairo size 350,262 enhanced font 'Verdana,10'"
SVG = "set terminal svg size 350,262 fname \
   'Verdana, Helvetica, Arial, sans-serif' fsize = 10"

With these settings and the use of macros our plotting will become more easier. For example to plot the sinusoid from the plotting functions introduction we can now use the following code.

@WXT
[...]
plot f(x) title 'sin(x)' with lines ls @BLUE, \
     g(x) notitle with lines ls @RED

Note that you have to omit the usage of reset in your code, because it will clear the line style settings.

Sinusoid

Fig. 1 Plotting using the configuration file (code to produce this figure)

You can of course override the style settings of the configuration file.

plot f(x) title 'sin(x)' with lines ls @BLUE, \
     g(x) notitle with lines ls @RED lw 1
Sinusoid

Fig. 1 Overwriting settings from the configuration file (code to produce this figure)