Search code examples
gnuplothistogramheatmap

3D view of 2D histogram (heat map) in gnuplot


How can I (or can I) present this gnuplot histogram:

enter image description here


in this style of 3D histogram view - in gnuplot?:

enter image description here


Using the same data and data format as in the gnuplot example in answer would be best.


Solution

  • Closest thing available in gnuplot that doesn't involve significant hackery and/or preprocessing seems to be impulses style. The help docs even suggest their use for 3d bar charts:

    To use this style effectively in 3D plots, it is useful to choose thick lines (linewidth > 1). This approximates a 3D bar chart.

    Here is a simple example using impulses, and matrix style data as in the linked heatmap example:

    set title ''
    unset key
    set xyplane 0
    set view 60,300,1.2
    set xrange [-0.5:5]
    set yrange [-0.5:5]
    splot '-' matrix with impulses lw 20
    0 0 0 0 0 0 
    0 1 2 3 4 5 
    0 2 4 6 8 10 
    0 3 6 9 12 15 
    0 4 8 12 16 20 
    0 5 10 15 20 25
    e
    

    enter image description here

    The issue is gnuplot renders the impulses as 2d pen strokes. It would be ideal if there was a way to some how apply a 3d surface effect to those lines, but I don't think there is a way, since they are just lines, not surfaces.

    You can also use vectors style to achieve similar result to impulses above, but with support for "rgb variable" (AFAIK impulses doesn't support this). This allows you to change color based on z-value - but still no 3d surface. You'll have to use a different data format for vectors style (AFAIK), but it's a simpler transform from matrix style data than some other hack require:

    set xyplane 0
    set view 60,301,1.2
    set xrange [0.5:5]
    set yrange [0.5:5]
    rgb(r,g,b) = 65536 * int(r) + 256 * int(g) + int(b)
    splot '-' using 1:2:(0):(0):(0):3:(rgb($3*(255/25),0,0)) with vectors nohead lw 40
    5 5 25
    5 4 20
    5 3 15
    5 2 10
    5 1 5
    4 5 20
    4 4 16
    4 3 12
    4 2 8
    4 1 4
    3 5 15
    3 4 12
    3 3 9
    3 2 6
    3 1 3
    2 5 10
    2 4 8
    2 3 6
    2 2 4
    2 1 2
    1 5 5
    1 4 4
    1 3 3
    1 2 2
    1 1 1
    e
    

    enter image description here