Contour plot
To create a contour plot like this two script files are needed. First of all a contour.gnu gnuplot file which plots the data and then contour.sh bash file which edits a temporary output file.
Steps in gnuplot:
- Write data into temporary file
- Write contour lines into temporary file
- Edit temporary file with contour.sh
- plot the edited data
Contour.sh
#!/bin/bash
gawk -v d=$2 -v w=$3 -v os=$4 'function abs(x) { return (x>=0?x:-x) }
{
if($0~/# Contour/) nr=0
if(nr==int(os+w/2) && d==0) {i++; a[i]=$1; b[i]=$2; c[i]=$3;}
if(abs(nr-os-w/2)>w/2 && d==1) print $0
nr++
}
END { if(d==0) {
for(j=1;j<=i;j++)
printf "set label %d \"%g\" at %g, %g centre front\n", j, c[j], a[j], b[j]
}
}' $1
d: which option (0: set labels, 1: give data back where no label is)
w: width
os: offset
Contour.gnu
# Step 1
set xrange [-5:0]
set yrange [2:5]
set isosample 150, 150
set table 'test.dat'
splot sin(1.3*x)*cos(.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)
unset table
# Step 2
set cont base
set cntrparam level incremental -3, 0.5, 3
unset surf
set table 'cont.dat'
splot sin(1.3*x)*cos(0.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)
unset table
reset
# Step 3 and 4
set xrange [-5:0]
set yrange [2:5]
unset key
set palette rgbformulae 33,13,10
load '<./contour.sh cont.dat 0 20 0'
plot 'test.dat' w ima, '<./contour.sh cont.dat 1 20 0' w l lt -1 lw 1.5
Step 1:
Surface data of the function "
sin(1.3*x)*cos(.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)
" is plotted into "test.dat" in the x- and y-range given.
Step 2:
By using "set cont base" only the contour data of the function is written to the file "cont.dat"
Step 3:
By loading the contour.sh script the contour data in cont.dat is read and the labels for gnuplot are created.
Step 4:
The plot command plots first the data as an image and adds to that the modified contour data (modified by contour.sh)
adapted from http://gnuplot-tricks.blogspot.com/2009/07/maps-contour-plots-with-labels.html