5

I have x- and y-data points representing a star cluster. I want to visualize the density using Gnuplot and its scatter function with overlapping points.

I used the following commands:

 set style fill transparent solid 0.04 noborder
 set style circle radius 0.01
 plot "data.dat" u 1:2 with circles lc rgb "red"

The result:

enter image description here

However I want something like that

enter image description here

Is that possible in Gnuplot? Any ideas?

Gilfoyle
  • 2,935
  • 1
  • 41
  • 74

3 Answers3

4

Probably, a much better way than my previous answer is the following. It takes about 2-3 minutes for 1000 points on my 8 year old computer. You basically can do the same thing for 3D (see https://stackoverflow.com/a/53744193/7295599)

The code:

### density color plot 2D
reset session

N = 1000     # number of datapoints
Delta = 0.5    # half boxwidth

TimeStart = time(0.0)
# create some dummy datablock with some distribution
set table $Data
    set samples N
    plot '+' u (invnorm(rand(0))):(invnorm(rand(0))) w table
unset table
print sprintf("Data generated: %.3f sec",time(0.0)-TimeStart)
# end creating dummy data

TimeStart = time(0.0)
# put the datafile/dataset into arrays
stats $Data nooutput
RowCount = STATS_records
array ColX[RowCount]
array ColY[RowCount]
array ColC[RowCount]
do for [i=1:RowCount] {
set table $Dummy
    plot $Data u (ColX[$0+1]=$1,0):(ColY[$0+1]=$2,0) with table
unset table
}
print sprintf("Data put into arrays: %.3f sec",time(0.0)-TimeStart)


# look at each datapoint and its sourrounding
do for [i=1:RowCount] {
    # print sprintf("Datapoint %g of %g",i,RowCount)
    x0 = ColX[i]
    y0 = ColY[i]
    # count the datapoints with distances <Delta around the datapoint of interest
    set table $Occurrences
        plot $Data u ((abs(x0-$1)<Delta) & (abs(y0-$2)<Delta) ? 1 : 0):(1) smooth frequency
    unset table
    # extract the number from $Occurrences which will be used to color the datapoint
    set table $Dummmy
        plot $Occurrences u (c0=$2,0):($0) every ::1::1 with table
    unset table
    ColC[i] = c0
}

# put the arrays into a dataset again
set print $Data
do for [i=1:RowCount] {
    print sprintf("%g\t%g\t%g",ColX[i],ColY[i],ColC[i])
}
set print
print sprintf("Duration: %.3f sec",time(0.0)-TimeStart)

set palette rgb 33,13,10
plot $Data u 1:2:3 w p ps 1 pt 7 lc palette z notitle
### end of code

will result in something like:

enter image description here

theozh
  • 17,412
  • 3
  • 21
  • 55
1

Would it be an option to postprocess the image with imagemagick?

# convert into a gray scale image
convert source.png -colorspace gray -sigmoidal-contrast 10,50% gray.png

# build the gradient, the heights have to sum up to 256
convert -size 10x1  gradient:white-white white.png
convert -size 10x85 gradient:red-yellow \
                    gradient:yellow-lightgreen \
                    gradient:lightgreen-blue \
        -append gradient.png
convert gradient.png white.png -append full-gradient.png

# finally convert the picture
convert gray.png full-gradient.png -clut target.png

I have not tried but I am quite sure that gnuplot can plot the gray scale image directly.

Here is the (rotated) gradient image: rotated gradient

This is the result: colored scatter plot

maij
  • 3,914
  • 2
  • 11
  • 28
1

Although this question is rather "old" and the problem might have been solved differently... It's probably more for curiosity and fun than for practical purposes. The following code implements a coloring according to the density of points using gnuplot only. On my older computer it takes a few minutes to plot 1000 points. I would be interested if this code can be improved especially in terms of speed (without using external tools). It's a pity that gnuplot does not offer basic functionality like sorting, look-up tables, merging, transposing or other basic functions (I know... it's gnuPLOT... and not an analysis tool).

The code:

### density color plot 2D
reset session

# create some dummy datablock with some distribution
N = 1000
set table $Data
    set samples N
    plot '+' u (invnorm(rand(0))):(invnorm(rand(0))) w table
unset table
# end creating dummy data

stats $Data u 1:2 nooutput
XMin = STATS_min_x
XMax = STATS_max_x
YMin = STATS_min_y
YMax = STATS_max_y
XRange = XMax-XMin
YRange = YMax-YMin
XBinCount = 20
YBinCount = 20
BinNo(x,y) = floor((y-YMin)/YRange*YBinCount)*XBinCount + floor((x-XMin)/XRange*XBinCount)

# do the binning
set table $Bins
    plot $Data u (BinNo($1,$2)):(1) smooth freq # with table
unset table

# prepare final data: BinNo, Sum, XPos, YPos
set print $FinalData
do for [i=0:N-1] {
    set table $Data3
    plot $Data u (BinNumber = BinNo($1,$2),$1):(XPos = $1,$1):(YPos = $2,$2) every ::i::i with table
    plot [BinNumber:BinNumber+0.1] $Bins u (BinNumber == $1 ? (PointsInBin = $2,$2) : NaN) with table
    print sprintf("%g\t%g\t%g\t%g", XPos, YPos, BinNumber, PointsInBin)
    unset table
}
set print

# plot data
set multiplot layout 2,1
set rmargin at screen 0.85
plot $Data u 1:2 w p pt 7 lc rgb "#BBFF0000" t "Data"
set xrange restore # use same xrange as previous plot
set yrange restore
set palette rgbformulae 33,13,10
set colorbox
# draw the bin borders
do for [i=0:XBinCount] {
    XBinPos = i/real(XBinCount)*XRange+XMin
    set arrow from  XBinPos,YMin to XBinPos,YMax nohead lc rgb "grey" dt 1
}
do for [i=0:YBinCount] {
    YBinPos = i/real(YBinCount)*YRange+YMin
    set arrow from  XMin,YBinPos to XMax,YBinPos nohead lc rgb "grey" dt 1
}

plot $FinalData u 1:2:4 w p pt 7 ps 0.5 lc palette z t "Density plot"
unset multiplot
### end of code

The result:

enter image description here

theozh
  • 17,412
  • 3
  • 21
  • 55