gnuplotheatmaptriangular

create a 2D heat map on a triangular mesh with gnuplot


Starting from the coordinates (two first columns) of two triangles and in the third column a temperature for each triangle, I want to draw with gnuplot the two triangles filled with colors associated to the third column (heatmap). The final goal is to use gnuplot to draw a heatmap from a list of triangles (triangular mesh)).

With this simple example, file1.dat:

# first triangle
0.0 0.0 10.
2.0 0.0 10.
1.0 2.0 10.

# second triangle
2.0 0.0 20.0
6.0 0.0 20.0
5.0 2.0 20.0

I expect gnuplot to draw the two triangles filled with colors associated with the third column. The code I ended up with is:

set pm3d map implicit # possibly with corners2color min
splot 'file1.dat' u 1:2:($3) with pm3d notitle

It leads to only one entity of a single color. image obtained with file1.dat

If I rearrange differently the file by having a blank line between each segments of the triangles: file2.dat:

#first triangle
0. 0. 10.0
2. 0. 10.0

2. 0. 10.0
1. 2. 10.0

1. 2. 10.0
0. 0. 10.0

#second triangle
2. 0. 20.0
6. 0. 20.0

6. 0. 20.0
5. 2. 20.0

5. 2. 20.0
2. 0. 20.0

I get a slightly better image but still not what I want (the second triangle color is not correct, only its segments are) image obtained with file2.dat

What am I missing here?


Solution

  • If you have a 2D plot, I would say there is no need to go to 3D, i.e. splot. With splot you might get some hassle with the graph/canvas margins which you don't have with a simple plot. Hence, simply use with filledcurves (check help filledcurves). There is the option closed. Although your data doesn't describe closed triangles, but gnuplot will close them with a straight line from the last to the first point. So, you can use the data as is from your first data example.

    Script:

    ### plotting filled triangles
    reset session
    
    $Data <<EOD
    # first triangle
    0.0 0.0 10.
    2.0 0.0 10.
    1.0 2.0 10.
    
    # second triangle
    2.0 0.0 20.0
    6.0 0.0 20.0
    5.0 2.0 20.0
    
    # third triangle
    3.0 1.0 30.0
    4.0 3.0 30.0
    1.5 1.5 30.0
    EOD
    
    set palette rgb 33,13,10
    set key noautotitle
    set style fill solid noborder
    
    plot $Data u 1:2:3 w filledcurves closed lc palette z
    ### end of script
    

    Result:

    enter image description here