Xatticus00
Xatticus00

Reputation: 146

Create 2D histogram (heat map) in gnuplot from raw (unbinned) data

I have a file with 2 columns of raw unbinned data, (x and y displacement). Each row in the file corresponds to a new time step. I would like to correlate the x and y displacements with each other.

For each row, I would like to bin the x-value and bin the y-value into histograms and then add one 'count' to the corresponding (x,y) bin.

I have a code that already does most of this, it bins each axis and then places a point/'count' in the corresponding space but the problem is that it doesn't add the 'counts' together. Instead, the points just overlap each other. You can kinda see how some of the points look bolder/darker than others where there are multiple points overlapping each other. Like so:

Gnuplot output with my full data set. Shows the bolder overlapping points:

enter image description here

My question is: how can I get points/'counts' that are placed on top of each other, (like in my image), to be added together instead so that I can see where the areas of higher correlation are with a color map or something like this?

I'd like my plot to look like this instead, with the color value corresponding to the number of points/'counts' in the 2D bin: enter image description here

My gnuplot code that kinda does what I want it to do:

#!/usr/local/bin/gnuplot

set term postscript eps enhanced color
set encoding utf8
set offsets
set style fill solid

bin(x,width,min)=width*(floor((abs(x) - min)/width) + 0.5)  + min

set output '2d.test.eps'
set palette defined (-1 "red", 0 "white", 1 "blue")
set cbrange [0:10]

binwidth=0.1
binwidth=0.1
xmin=-360
ymin=-360
set boxwidth binwidth*0.75

set view map
splot '2d.dat' using (bin($1,binwidth,xmin)):(bin($2,binwidth,ymin)):(1.0) not

Data file format:

   0.864    3.868
   1.878    3.617
   0.138    2.787
   0.646    3.220
  -0.969    3.176
  -0.447    3.031
  -0.316    3.130
   0.205    3.342
  -1.127    3.661
  -1.349    3.702
...

Upvotes: 4

Views: 1637

Answers (1)

mrclng
mrclng

Reputation: 543

If you are willing to do the calculation with awk and the counting with uniq you can use:

awk 'function abs(x){return (sqrt(x*x))}{print 0.1*(int((abs($1)+360)/0.1)+0.5)-360" "0.1*(int((abs($2)+360)/0.1)+0.5)-360}' input.dat | sort | uniq -c > output.dat

Since uniq prepends the count you need to plot this like

set view map
splot "output.dat" 2:3:1 matrix with image

This should also be possible directly from within gnuplot:

splot "<awk 'function abs(x){return (sqrt(x*x))}{print 0.1*(int((abs($1)+360)/0.1)+0.5)-360\" \"0.1*(int((abs($2)+360)/0.1)+0.5)-360}' data.dat | sort | uniq -c" u 2:3:1  2:3:1 matrix with image

(note the escaped " chars!)

Upvotes: 1

Related Questions