Assume we have a 5x4x3 3D grid, where each cell has a value:
0.5523 0.0495 0.1465 0.5386
0.6299 0.4896 0.1891 0.6952
0.0320 0.1925 0.0427 0.4991
0.6147 0.1231 0.6352 0.5358
0.3624 0.2055 0.2819 0.4452
0.1239 0.2085 0.9479 0.6210
0.4904 0.5650 0.0821 0.5737
0.8530 0.6403 0.1057 0.0521
0.8739 0.4170 0.1420 0.9312
0.2703 0.2060 0.1665 0.7287
0.7378 0.8589 0.1339 0.3329
0.0634 0.7856 0.0309 0.4671
0.8604 0.5134 0.9391 0.6482
0.9344 0.1776 0.3013 0.0252
0.9844 0.3986 0.2955 0.8422
How can I achieve a plot in gnuplot that is similar to this plot achieved in matlab using patch
? Note that the grid cells has an alpha value of 0.8.
This solution makes use of the latest version of Gnuplot (6.0.2).
By using the "splot ... with polygons" style, unit voxels are placed on the desired 3D grid. By assigning appropriate colors to each voxel, a 3D heatmap can be rendered.
The script is as follows:
$data <<EOD
0.5523 0.0495 0.1465 0.5386
0.6299 0.4896 0.1891 0.6952
0.0320 0.1925 0.0427 0.4991
0.6147 0.1231 0.6352 0.5358
0.3624 0.2055 0.2819 0.4452
0.1239 0.2085 0.9479 0.6210
0.4904 0.5650 0.0821 0.5737
0.8530 0.6403 0.1057 0.0521
0.8739 0.4170 0.1420 0.9312
0.2703 0.2060 0.1665 0.7287
0.7378 0.8589 0.1339 0.3329
0.0634 0.7856 0.0309 0.4671
0.8604 0.5134 0.9391 0.6482
0.9344 0.1776 0.3013 0.0252
0.9844 0.3986 0.2955 0.8422
EOD
#
# flatten matrix data into (x,y,z,value) data
#
set table $flatten
plot $data matrix using 2:1:(column(-2)):3 with table
unset table
#
# unit voxel data at (0,0,0)
#
$cell <<EOD
0 0 0
0 1 0
1 1 0
1 0 0
0 0 0
0 0 1
0 1 1
0 1 0
0 0 0
0 0 1
0 1 1
0 1 0
0 0 1
0 1 1
1 1 1
1 0 1
1 0 0
1 0 1
1 1 1
1 1 0
0 0 0
1 0 0
1 0 1
0 0 1
0 1 0
1 1 0
1 1 1
0 1 1
EOD
#
# construct data for voxel at each point in $flatten
#
# shift unit voxel at (0,0,0) to (x,y,z)
#
set print $voxeldata
do for [k=1:|$flatten|] {
pv = split($flatten[k])
do for [i=1:|$cell|] {
if ($cell[i] eq "") {
print "\n"
} else {
pc = split($cell[i])
print sprintf("%g %g %g %g\n", \
real(pv[1]+pc[1]), real(pv[2]+pc[2]), real(pv[3]+pc[3]), real(pv[4]))
}
}
print "\n"
}
unset print
#
# plot $voxeldata with polygons
#
set isotropic
set xrange [0:5]
set yrange [0:4]
set zrange [0:3]
set xyplane relative 0
set xtics 1 out
set ytics 1 out
set ztics 1 out
set cbtics nomirror
set palette defined (0 "black", 1 "white")
set cbrange [0:1]
set colorbox origin screen 0.8,0.2 size 0.05,0.8
unset key
set pm3d depthorder
set hidden3d nooffset
set lmargin screen 0.1
set rmargin screen 0.9
set bmargin screen 0.1
set tmargin screen 0.9
set view 240,320
splot $voxeldata using 1:2:3:(palette($4)) \
with polygons fs transparent solid 0.8 fc rgb variable, \
NaN w p palette