#
# Various ways to create a 2D heat map from ascii data
#

set title "Heat Map generated from a file containing Z values only"
unset key
set tic scale 0

# Color runs from white to green
set palette rgbformula -7,2,-7
set cbrange [0:5]
set cblabel "Score"
unset cbtics

set xrange [-0.5:4.5]
set yrange [-0.5:4.5]

$map1 << EOD
5 4 3 1 0
2 2 0 0 1
0 0 0 1 0
0 0 0 2 3
0 1 2 4 3
EOD

set view map
splot '$map1' matrix with image

pause -1 "Hit return to continue"

set title "Heat Map generated by 'plot' from a stream of XYZ values"\
         ."\nNB: Rows must be separated by blank lines!"

$map2 << EOD
0 0 5
0 1 4
0 2 3
0 3 1
0 4 0

1 0 2
1 1 2
1 2 0
1 3 0
1 4 1

2 0 0
2 1 0
2 2 0
2 3 1
2 4 0

3 0 0
3 1 0
3 2 0
3 3 2
3 4 3

4 0 0
4 1 1
4 2 2
4 3 4
4 4 3
EOD 

plot '$map2' using 2:1:3 with image

pause -1 "Hit return to continue"

set title "Heat map with non-zero pixel values written as labels"
plot $map1 matrix using 1:2:3 with image, \
     $map1 matrix using 1:2:($3 == 0 ? "" : sprintf("%g",$3) ) with labels

pause -1 "Hit return to continue"

$sparsedata << EOD
# incomplete data for a 5x5 grid
0 0 5
0 1 4
0 2 3
0 3 1
2 3 1
3 3 2
3 4 3
4 1 1
4 2 2
4 3 4
4 4 3
1 0 2
1 1 2
1 4 1
EOD

set title "Same data input as a sparse matrix (non-zero values only)"
set palette maxcolors 6
set cbtics 0,1,5 scale 2 offset -3.5, 1.5
set cbrange [0:6]
plot $sparsedata sparse matrix=(5,5) using 2:1:3 with image

pause -1 "Hit return to continue"

set title "Sparse matrix handling is also possible with splot"
splot $sparsedata sparse matrix=(5,5) using 2:1:3 with image

pause -1 "Hit return to continue"

set title "Heat map from csv data with column and row labels"

$map3 << EOD
,Apple,Bacon,Cream,Donut,Eclair
Row 1, 5, 4, 3, 1, 0
Row 2, 2, 2, 0, 0, 1
Row 3, 0, 0, 0, 1, 0
Row 4, 0, 0, 0, 2, 3
Row 5, 0, 1, 2, 4, 3
EOD

set datafile separator comma
plot '$map3' matrix rowheaders columnheaders using 1:2:3 with image
set datafile separator

pause -1

# Some output modes (SVG, HTML5) allow smoothing of adjacent pixels,
# which may not be desired. This is not normally an issue with PNG or PDF.
# The `pixels` option forces pixel-by-pixel drawing with no smoothing.

unset colorbox
set palette maxcolors 0
set format xy ""
set multiplot layout 1,2 title "Compare 'image' and 'image pixels' modes" \
    margin screen 0.05, 0.95, 0.10, 0.85 spacing screen 0.05
set title "plot with image"
plot $map1 matrix using 1:2:3 with image
set title "plot with image pixels"
plot $map1 matrix using 1:2:3 with image pixels
unset multiplot

pause -1 "Hit return to continue"

# Release datablocks used in this demo
undefine $map1 $map2 $map3
reset
