Search code examples
gnuplotheatmap

Gnuplot heat map using pm3d and dgrid3d leaves white spaces in plot


Im trying to generate a heat map like plot from data. The data is given in the form of

# x      y      f(x,y)
 1.02   0.98    23.943
 1.99   0.99    24.05
 .99    2.01    22.00
...

and there is no real grid on it, they are just some random points and their specific value. Otherwise I could use things like image as far as I understand.

To illustrate my problem, I created a data file for a 2d gaussian peak (see pastebin). Data illustrated:

simple splot of data file

Now plotting this using

set view map
set dgrid3d 50,50,2
set pm3d at b
unset key
unset surface
splot "data.txt"    # using 2d gauss data as example

gives me a plot like the following:

splot using pm3d, dgrid3d, map

What I don't understand is the white spaces between the coloured surface and the actual border of the plot. Here shown at the bottom, the top, the right and the left. Even if I zoom in to ranges, where the surface should be created by using dgrid3d, the white spaces still are there with different width.

Trying to plot now (with settings above)

set isosample 100,100
splot exp(-x**2 -y**2)

gives me a plot that fills the entire plotting area. No matter how far I zoom in or out. No white spaces appear anywhere. See:

function plot using pm3d dgrid3d

I found this post here (Annoying spacing in gnuplot pm3d color maps) where op replied that even by plotting a function the spaces remain:

I just tried plotting a simple exp(-x²) but the gap is still there. :/ – stonebird Jun 6 '16 at 10:31

He, however, could resolve this by defining a very specific range for the xand yvalues. But this is not working for me since it's realy difficult to find out for which values this actually works.

Is there a possibility of creating a heat map like plot from data points that fills the entire plotting area?

Using gnuplot 5.0 patchlevel 6


Solution

  • That's imo an inherent problem with the gridding (+ a similar one with pm3d). From help dgrid3d :

    A grid with dimensions derived from a bounding box of the scattered data and size as specified by the row/col_size parameters is created for plotting and contouring.

    Gnuplot first cuts out all datapoints that are within your x/yranges, then makes a bounding box on those (which likely is a bit smaller), and only then creates the gridded data.

    Ideally one should be able to switch off this creation of a bounding box and have gnuplot just make a grid spanning the whole x/yrange.

    The way around this is to plot the gridded data to a file (or, since gp5, to an inline $datablock) with set table, then switch off dgrid3d, and plot this datablock. Then you still have to zoom exactly to min/max x/y values that have a datapoint (to get rid of the whitespace), but you can at least easily look those up.

    Oooor skip the datablock creation and automatically cut the zoomed-in view:

    1. plot the dataset with pm3d / dgrid3d
    2. zoom in to what area you want to have
    3. now cut off the white space by doing

      set xrange [GPVAL_DATA_X_MIN:GPVAL_DATA_X_MAX]
      set yrange [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
      replot
      

    Problem is you again might have artifacts at the border, so perhaps you still want to separate the gridding from the plotting as explained above.

        set dgrid3d
        set table $datagrid
        splot dataf
        unset table
        unset dgrid3d
    
        set pm3d; set view map
        splot $datagrid
        #now zoom to whatever view you would like to see
    
        set xrange [GPVAL_DATA_X_MIN:GPVAL_DATA_X_MAX]
        set yrange [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
        replot
    

    (also look into help dgrid3d to choose a suitable interpolation algorithm/kernel)