Binning of 2D data is the same principle as for 1D. The special point about it is that the option smooth freq
which is used for 1D binning will only accept one value for the bins (not two: x and y).
Hence, you simply enumerate your bins from 0
to BinCountX * BinCountY - 1
and define functions BinValueX(), BinValueY()
to get back from your bin number to the x- and y-bin values.
The test data section creates random x,y and z-values. The z-values within a x,y-bin will be added during the binning process.
Alternatively, depending on the data, a density plot could also be of interest.
Script: (works with gnuplot>=5.0.0)
### 2D binning of data
reset session
# create some random test data
set table $Data
set samples 5000
plot '+' u (invnorm(rand(0))):(invnorm(rand(0))):(int(rand(0)*10+1)) w table
set samples 1000
plot '+' u (invnorm(rand(0))+2):(invnorm(rand(0))+2):(int(rand(0)*10+1)) w table
unset table
BinWidthX = 0.25
BinWidthY = 0.25
# get data range min, max
stats $Data u 1:2 nooutput
Xmin = floor(STATS_min_x/BinWidthX)*BinWidthX
Ymin = floor(STATS_min_y/BinWidthY)*BinWidthY
Xmax = ceil(STATS_max_x/BinWidthX)*BinWidthX
Ymax = ceil(STATS_max_y/BinWidthY)*BinWidthY
BinCountX = int((Xmax-Xmin)/BinWidthX)
BinCountY = int((Ymax-Ymin)/BinWidthY)
BinNoX(x) = floor((x-Xmin)/BinWidthX)
BinNoY(y) = floor((y-Ymin)/BinWidthY)
BinNo(x,y) = BinNoY(y)*BinCountX + BinNoX(x)
BinValueX(n) = Xmin + (int(n)%BinCountX)*BinWidthX
BinValueY(n) = Ymin + (int(n)/BinCountY)*BinWidthY # integer division!
# get data into bins
set table $Bins
plot [*:*][*:*] $Data u (BinNo($1,$2)):3 smooth freq
unset table
set size ratio -1
set xrange [Xmin:Xmax]
set yrange [Ymin:Ymax]
set key noautotitle
set view 60,30,1.2
set style fill solid 1.0
set grid x,y
set multiplot layout 1,2
set title "Raw data"
plot $Data u 1:2:3 w p pt 7 ps 0.2 lc palette
set title "2D binned data"
plot $Bins u (BinValueX($1)+BinWidthX/2.):(BinValueY($1)+BinWidthY/2.):\
(BinWidthX/2.):(BinWidthX/2.):2 w boxxy fc palette z
unset multiplot
### end of script
Result:
