1
votes

I am trying to convert a plot we normally do using points to a heatmap. The data is in x,y,z format with x,y being lat / lon and z being the # of recorded instances

We currently plot using:

plot 'data.dat' u 1:2:3 with points pointtype 13 lc palette, \
'boundary.dat' with lines ls 1

I would like to be able to plot a heatmap as 3 plots: 1. base heatmap of values 2. points at maybe 50% opacity to show underlying geo path 3. line representing the boundary.

sample image:enter image description here The image on the left is gnuplot, the one on the right is from qgis. We would like gnuplot to produce something similar to the one on the right.

Peter

1

1 Answers

2
votes

Nice question :)

You'll need a few steps to get a proper result:

In order to get a heatmap, you must create a regular grid and interpolate your data. This can be done with set dgrid3d. Because you want the interpolation only for some of your data you must write the interpolation to a temporary file:

set dgrid3d 200,200,2
set table 'data-heatmap.dat'
splot '< tr "," " " < data.dat' using 1:2:3
unset table
unset dgrid3d

Usually, you can use set datafile separator ',' to use commas as delimiters, but the temporary file data-heatmap.dat' contains columns separated by spaces, so you must usetr` to replace the commas with white spaces.

Then you can plot all together. A complete script might look as follows:

# Diverging color palette from colorbrewer2.org
set palette defined (0 '#006837', 1 '#1a9850', 2 '#66bd63',\
                     3 '#a6d96a', 4 '#d9ef8b', 5 '#ffffbf',\
                     6 '#fee08b', 7 '#fdae61', 8 '#f46d43',\
                     9 '#df3027', 10 '#a50026')
unset key
set dgrid3d 200,200,2
set table 'data-heatmap.dat'
splot '< tr "," " " < data.dat' using 1:2:3
unset table
unset dgrid3d

set autoscale fix
set xtics 0.1
plot 'data-heatmap.dat' u 1:2:3 w image,\
     '< tr "," " " < boundary.dat' w l lc rgb 'black' lw 2,\
     '< tr "," " " < data.dat' w p pt 7 ps 0.2 lc palette

To get a smoother interpolation, use set dgrid3d 200,200,1 or similar. Maybe you also want to set a fixed color range with set cbrange.

The result with 4.6.5 is:

enter image description here

The dimensions of the generated grid is derived from the bounding box of the scattered data. As a hack to extend the region of interpolation you can add two dummy points on-the-fly when creating the interpolated data: one point at the upper right and one at the upper left. Later you can correct a bit for these points with set offsets:

# Diverging color palette from colorbrewer2.org
set palette defined (0 '#006837', 1 '#1a9850', 2 '#66bd63',\
                     3 '#a6d96a', 4 '#d9ef8b', 5 '#ffffbf',\
                     6 '#fee08b', 7 '#fdae61', 8 '#f46d43',\
                     9 '#df3027', 10 '#a50026')
unset key
set dgrid3d 200,200,2
set table 'data-heatmap.dat'
splot '< echo -e "\n-118.62,34.29,1\n-118.10,34.29,1" | paste -s -d "\n" data.dat - | tr "," " "' using 1:2:3
unset table
unset dgrid3d

set autoscale fix
set offsets -0.02,-0.01,-0.01,0.01
set xtics 0.1
plot 'data-heatmap.dat' u 1:2:3 w image,\
     '< tr "," " " < boundary.dat' w l lc rgb 'black' lw 2,\
     '< tr "," " " < data.dat' w p pt 7 ps 0.2 lc palette

enter image description here