Gnuplot Multi Column fit (not Multi-branch) - gnuplot

I have data files "y.csv" which contains several runs (data sets) of an experiment in columns that I want to simultaneously fit to a single function. It should work like plot for [i=2:*] "y.csv" using 1:i
to automatically accomodate however many columns are in the file. Here is a short example data file:-
,B,C,D,E,F,G,H
01,,,,,,,
02,0.2200,0.2200,0.2080,0.2170,0.1530,,
03,0.2720,0.3230,0.2530,0.2380,0.2620,,
04,0.3900,0.3790,0.3770,0.3760,0.3500,,
05,0.5520,0.5600,0.5450,0.4830,0.4870,,
06,0.6640,0.6300,0.6830,0.6030,0.6520,,
07,0.6440,0.6900,0.6360,0.5960,0.6520,,
08,0.6030,0.6470,0.6190,0.6300,0.6280,,
09,0.5450,0.5890,0.5860,0.6830,0.5540,,
10,0.6370,0.6430,0.5800,0.5270,0.6180,,
11,0.6400,0.5600,0.7190,0.6780,0.7420,,
12,,,,,,,
I can automatically plot each of these columns, overlooking column headers, etc with:-
set datafile separator ","
set datafile columnheaders
set key autotitle columnheader
set key top left
set key title "Run"
set xrange [1:12]
set xlabel "Dilution (Proportional to log([]) )"
set ylabel "Response"
plot for [i=2:*] "y.csv" using 1:i with linespoints
I can set up a function to fit with the following:-
sig(x) = 1 / (1+exp(-x)) ; # Appears stable enough in gnuplot
A = 0.6 ; # Sigmoid Amplitude
B = 0.2 ; # Sigmoid offset
C = 6 ; # Center shift on displayed X axis
K = 1 ; # Shape factor
ssig(x) = B + A*sig(K*(x-C)) ; # Fit to this
And, I can fit to the first data column with:-
fit ssig(x) "y.csv" using 1:2 via A,B,C,K
But I can't work out the syntax of how to automatically do this over all the columns like I can for plotting. I was expecting something like
fit [1:-1:i=2:*] ssig(x) "y.csv" using 1:i via A,B,C,K
would iterate over the columns. I just don't understand the multi-branch syntax, and guess I am missing some simple concept.
Many thanks

Based on your comment you are actually not searching for a multi-branch fit, but you want to merge all columns into one single data set and perform a fit using all data points at the same time. This can be achieved quite easily by reshaping the data file into a datablock first:
set datafile separator ","
set table $FITDATA
plot for [i=2:*] "y.csv" u 1:i
unset table
unset datafile separator
sig(x) = 1 / (1+exp(-x)) ; # Appears stable enough in gnuplot
A = 0.6 ; # Sigmoid Amplitude
B = 0.2 ; # Sigmoid offset
C = 6 ; # Center shift on displayed X axis
K = 1 ; # Shape factor
ssig(x) = B + A*sig(K*(x-C)) ; # Fit to this
set fit errorvariables
fit ssig(x) $FITDATA u 1:2 via A,B,C,K
In the datablock the columns are separated by tabs, not comma, therefore one has to revert the datafile separator to default while fitting, and change it back again for plotting. Maybe someone else has a cleaner solution for this. set fit errorvariables saves the fit errors, so that they can be used for the plot title later.
set datafile separator ","
set datafile columnheaders
set key autotitle columnheader
set key top left
set key title "Run"
set xrange [1:12]
set xlabel "Dilution (Proportional to log([]) )"
set ylabel "Response"
plot for [i=2:*] "y.csv" u 1:i w lp, \
ssig(x) lc black lw 3 t "fit", \
keyentry t sprintf("A = %.3f ± %.3f", A, A_err), \
keyentry t sprintf("B = %.3f ± %.3f", B, B_err), \
keyentry t sprintf("C = %.3f ± %.3f", C, C_err), \
keyentry t sprintf("K = %.3f ± %.3f", K, K_err)

Related

Place tics at values stored in variables

I have used the stats command to store the x-postion of absolute maxima in my plot of seven datasets in seven variables, grN_pos_max_y with N that goes from 1 to 7. Can I place the tics in the x-axis at the positions specified by these variables?
I tried using
$maxima << EOD
gr1_pos_max_y
gr2_pos_max_y
gr3_pos_max_y
gr4_pos_max_y
gr5_pos_max_y
gr6_pos_max_y
gr7_pos_max_y
EOD
and then
plot ..., \
$maxima u 1:(NaN):xticlabel(1) notitle
but I don't know how to read variables into a data block (if I replace the variable names by their values, however, it works).
Edit: This is what I want (I plotted it using Ethan's answer)
I'm not entirely sure I understand what you want, but this may get you partway there:
set xtics add (gr1_pos_max_y, gr2_pos_max_y, gr3_pos_max_y, gr4_pos_max_y, gr5_pos_max_y, gr6_pos_max_y, gr7_pos_max_y)
plot 'whatever'
That will get you plain (unlabeled) tic marks in addition to whatever tic marks and labels are being generated automatically.
If you want only these marks and no auto-generated marks, remove the keyword add.
If you want to place labels to go with these new tics, change it to:
set xtics add ( "Max 1" gr1_pos_maxy, "Max 2" gr2_pos_maxy, ...
This is all assuming you want these tics to label a plot that contains something other than the tics themselves. If you want only a plot of these y values, perhaps as impulses?, please re-phrase the question or show a sketch of what you want it to look like.
There is no need for awk, you can do it all in gnuplot.
put stats into a loop and write the STATS values into a datablock $Maxima
plot your data and $Maxima as Ethan suggested with impulses
you can also plot the maxima y-value as labels in the graph
The script needs to be adapted depending on your file naming scheme.
Script:
### extract maxima from several files
reset session
N = 7
myFile(n) = sprintf("SO72750257_%d.dat",n)
# create some "random" test data
do for [n=1:N] {
set table myFile(n)
f(x) = -a*(x-x0)**2 +y0
x0 = (n-1)*10./N + rand(0)*10./N
a = rand(0)*50+10
y0 = rand(0)*80+20
plot [0:10] '+' u 1:(f(x))
unset table
}
# extract maxima
set print $Maxima
do for [n=1:N] {
stats myFile(n) u 1:2 nooutput
print sprintf("%.1f %.1f", STATS_pos_max_y, STATS_max_y)
}
set print
set yrange[0:]
set offsets graph 0.05, graph 0.05, graph 0.1, 0
set xtics () # remove all xtics
set key out noautotitle
plot for [i=1:N] myFile(i) u 1:2 w l ti sprintf("Set %d",i), \
$Maxima u 1:2:($0+1):xtic(1) w impulses lc var dt 2, \
$Maxima u 1:2:2 w labels offset 0, char 1
### end of script
Result:

How to add data labels to Gnuplot histogram (smooth freq)?

I have data of protein molecular weights in column 6 of my file. The column in question looks like this:
MW [kDa]
16.8214045562515
101.41770820613989
24.332255496943485
43.946599899844436
210.58276787970942
57.987597263605494
27.384315650885558
119.02857910337919
8.962938979036466
I would like to plot a histogram and I am doing it using Gnuplot's smooth frequency function:
echo n=20 >$gnuplot #number of intervals
echo max=100 >> $gnuplot #max value
echo min=-0 >> $gnuplot #min value
echo width=\(max-min\)\/n >> $gnuplot #interval width
echo hist\(x,width\)=width*floor\(x\/width\)+width\/2.0 >> $gnuplot
echo plot \"$dataFile\" using \(hist\(\$6,width\)\)\:\(1.0\) smooth freq w boxes lc rgb\"blue\" notitle >> $gnuplot
How do I add a data label representing the count for each bin on top of each histogram bar? I cannot seem to find a way to do it.
I would plot the histogram data into a table first and then use this table for plotting the histogram itself and the labels.
Check the following example. If you have a file, e.g. 'myData.dat', skip the random data generation lines, instead add the line FILE = 'myData.dat' and replace all $Data with FILE. As #Eldrad mentioned in the comments, use the plotting style with labels for the labels. Check help labels and help table.
Code:
### histogram with labeled bins
reset session
# create some random test data
set print $Data
do for [i=1:2000] {
print sprintf("%g",(invnorm(rand(0))+10)*20)
}
set print
stats $Data u 1 nooutput
xmin = STATS_min
xmax = STATS_max
N = 20
myWidth = (xmax-xmin)/N
bin(col) = myWidth*floor(column(col)/myWidth)+myWidth/2.
set key noautotitle
set style fill solid 0.3
set boxwidth myWidth
set grid x,y
set offsets graph 0,0,0.05,0 # l,r,t,b
set table $Histo
plot $Data u (bin(1)) smooth freq
unset table
plot $Histo u 1:2 w boxes lc rgb "blue", \
'' u 1:2:2 w labels offset 0,0.7
### end of code
Result:

Gnuplot ellipsoids with Text file center

I have a text file with 3 columns defining 3D points.
I want to paint every point in 3D and an ellipsoid centered in every point. I discard using the
set parametric
way because I need to iterate my text file.
So i think in doing something like this:
gnuplot
reset
set xrange [-5:5]
set yrange [-5:5]
set zrange [-5:5]
Rx = 1
Ry = 1
Rz = 1
fx(u,v) = column(2) + Rx*cos(u)*cos(v)
fy(u,v) = column(1) + Ry*sin(u)*cos(v)
fz(u,v) = column(3) + Rz*sin(v)
iMax = 200
splot "file.txt" using ($2):($1):($3) title "Input " with points ps 2 pt 7,\
for [i=0:iMax] "file.txt" u (fx(2*pi*i/iMax, pi*i/iMax)):(fy(2*pi*i/iMax, pi*i/iMax)):(fz(2*pi*i/iMax, pi*i/iMax)) notitle with points ps 2 pt 7
But the only think I can get is this strange and heavy (I know that they are a lot of iterations per row, but maybe there is another approach) pattern
Any help? Thank you.
There is something wrong for the mathematic point of view? Using something like this Im perfectly able to plot spheres, but without parsing data:
set parametric
R = 1
set urange [-pi/2:pi/2]
set vrange [0:2*pi]
splot R*cos(u)*cos(v),R*cos(u)*sin(v),R*sin(u) w l lc rgb "yellow"
I assume you want to plot the 2D surfaces of 3D ellipsoids. But the plot command has only a loop over i which is only 1D. This can not give a 2D surface. It might be possible to nest another 1D loop to get this approach to work.
I would suggest something else. Before plotting, you can store the center coordinates into a gnuplot array. Then you loop over this array and plot a sphere/ellipsoid using parametric mode.
This might be a starting point:
# This is the file with the center coordinates.
datafile = "ellipses.dat"
# The "stats" command stores the number of rows in the STATS_records variable.
stats datafile nooutput
num_rows = STATS_records
# Generate arrays which will contain the center coordinates of the ellipsoids.
array centers_x[num_rows]
array centers_y[num_rows]
array centers_z[num_rows]
# Read the center coordinates into the prepared arrays.
# I "misuse" the stats command. The "using" expression in parenthesis executes
# the respective commands and returns the value after the last comma: row + 1.
# This return value is not needed anywhere.
row = 1
stats datafile using (centers_x[row]=$1, \
centers_y[row]=$2, \
centers_z[row]=$3, \
row = row + 1) nooutput
# Output into an image file.
set terminal pngcairo
set output "ellipsoids.png"
# Set parameters for ellipsoids.
Rx = 0.1
Ry = 0.1
Rz = 0.7
# Use parametric mode for plotting.
set parametric
set urange [-pi/2:pi/2]
set vrange [0:2*pi]
# Finally plot:
splot datafile using 1:2:3 title "Input " with points ps 2 pt 7, \
for [i=1:num_rows] centers_x[i] + Rx*cos(u)*cos(v), \
centers_y[i] + Ry*cos(u)*sin(v), \
centers_z[i] + Rz*sin(u) notitle
Please doublecheck x, y, and z: I was not that careful. This is the result:
I have used this example data:
1 2 3
2 2 4
2 3 4
3 3 3
3 4 5
Arrays are available starting with gnuplot 5.2. For older versions, please search the internet for workarounds.

gnuplot contour plot hatched lines

I'm using gnuplot for contour plot of a several function. This is for optimization problem.
I have 3 functions:
f(x,y)
g1(x,y)
g2(x,y)
both g1(x,y) and g2(x,y) are constraints and would like to plot on top of the contour plot of f(x,y).
Here is the textbook example:
Here is my attempt to replicate it in gnuplot, thanks to #theozh.
### contour lines with labels
reset session
f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
g1(x,y)=(x-5)**2+y**2
g2(x,y) = 4*x+y
set xrange [0:6]
set yrange [0:6]
set isosample 250, 250
set key outside
set contour base
set cntrparam levels disc 10,30,75,150,300,500,850,1500
unset surface
set table $Contourf
splot f(x,y)
unset table
set contour base
set cntrparam levels disc 26
unset surface
set table $Contourg1
splot g1(x,y)
unset table
set contour base
set cntrparam levels disc 20
unset surface
set table $Contourg2
splot g2(x,y)
unset table
set style textbox opaque noborder
set datafile commentschar " "
plot for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5)
replot $Contourg1 u 1:2:(1) skip 5 index 0 w l lw 4 lc 0 title columnheader(5)
replot $Contourg2 u 1:2:(1) skip 5 index 0 w l lw 4 lc 0 title columnheader(5)
I would like to replicate the textbook picture in the gnuplot example. How to do a hatch mark on the functions g1 and g2, the thick black line in plot above.
#theozh provided an excellent solution below. However, the method doesnot work for steep curves. As an example
reset session
unset key
set size square
g(x,y) = -0.8-1/x**3+y
set xrange [0:4]
set yrange [0:4]
set isosample 250, 250
set key off
set contour base
unset surface
set cntrparam levels disc 0
set table $Contourg
splot g(x,y)
unset table
set angle degree
set datafile commentschar " "
plot $Contourg u 1:2 skip 5 index 0 w l lw 2 lc 0 title columnheader(5)
set style fill transparent pattern 4
replot $Contourg u 1:2:($2+0.2) skip 5 index 0 w filledcurves lc 0 notitle
yields the following figure. Is there a way to use different offsets, for example offset x values for x < 1.3 and for x > 1.3 offset y values. This would yield a much better filled curve. A matlab implementations of what I was looking for can be found here: https://www.mathworks.com/matlabcentral/fileexchange/29121-hatched-lines-and-contours.
In replcating #Ethans program, I get the following, the dashtype is relatively thick compared to #Ethan not sure why, I'm using gnuplot v5.2 and wxt terminal.
When I replicate #theozh code, it works very well except for closed contours, not sure why? see below for example:
f(x,y)=x*exp(-x**2-y**2)+(x**2+y**2)/20
g1(x,y)= x*y/2+(x+2)**2+(y-2)**2/2-2
set xrange [-7:7]
set yrange [-7:7]
set isosample 250, 250
set key outside
set contour base
unset surface
set cntrparam levels disc 4,3.5,3,2.5,2,1.5,1,0.5,0
set table $Contourf
splot f(x,y)
unset table
set cntrparam levels disc 0
set table $Contourg1
splot g1(x,y)
unset table
# create some extra offset contour lines
# macro for setting contour lines
ContourCreate = '\
set cntrparam levels disc Level; \
set table #Output; \
splot #Input; \
unset table'
Level = 0.45
Input = 'g1(x,y)'
Output = '$Contourg1_ext'
#ContourCreate
# Macro for ordering the datapoints of the contour lines which might be split
ContourOrder = '\
stats #DataIn skip 6 nooutput; \
N = STATS_blank-1; \
set table #DataOut; \
do for [i=N:0:-1] { plot #DataIn u 1:2 skip 5 index 0 every :::i::i with table }; \
unset table'
DataIn = '$Contourg1'
DataOut = '$Contourg1_ord'
#ContourOrder
DataIn = '$Contourg1_ext'
DataOut = '$Contourg1_extord'
#ContourOrder
# Macro for reversing a datablock
ContourReverse = '\
set print #DataOut; \
do for [i=|#DataIn|:1:-1] { print #DataIn[i]}; \
set print'
DataIn = '$Contourg1_extord'
DataOut = '$Contourg1_extordrev'
#ContourReverse
# Macro for adding datablocks
ContourAdd = '\
set print #DataOut; \
do for [i=|#DataIn1|:1:-1] { print #DataIn1[i]}; \
do for [i=|#DataIn2|:1:-1] { print #DataIn2[i]}; \
set print'
DataIn1 = '$Contourg1_ord'
DataIn2 = '$Contourg1_extordrev'
DataOut = '$Contourg1_add'
#ContourAdd
set style fill noborder
set datafile commentschar " "
plot \
for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5), \
$Contourg1 u 1:2 skip 5 index 0 w l lw 2 lc 0 title columnheader(5), \
$Contourg1_add u 1:2 w filledcurves fs transparent pattern 5 lc rgb "black" notitle
Another possibility is to use a custom dash pattern, as shown below:
By the way, it is almost never correct to use "replot" to compose a single figure.
# Additional contour levels displaced by 0.2 from the original
set contour base
set cntrparam levels disc 20.2
unset surface
set table $Contourg2d
splot g2(x,y)
unset table
set contour base
set contour base
set cntrparam levels disc 26.2
unset surface
set table $Contourg1d
splot g1(x,y)
unset table
set linetype 101 lc "black" linewidth 5 dashtype (0.5,5)
plot for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5), \
$Contourg1 u 1:2:(1) skip 5 index 0 w l lw 1 lc "black" title columnheader(5), \
$Contourg2 u 1:2:(1) skip 5 index 0 w l lw 1 lc "black" title columnheader(5), \
$Contourg1d u 1:2:(1) skip 5 index 0 w l linetype 101 notitle, \
$Contourg2d u 1:2:(1) skip 5 index 0 w l linetype 101 notitle
Amended to show use of contours offset so that the dashes are only on one side of the line.
Here is the solution you (and I) were hoping for.
You just enter the hatch parameters into a datablock: TiltAngle in degrees (>0°: left side, <0° right side in direction of path), HatchLength and HatchGap in pixels. The procedure has become a bit lengthy but it does what you want. I have tested it with gnuplot 5.2.8 and 5.4.1 and wxt and qt terminal.
What the procedure basically does:
determines the angle between two consecutive points of the data input curve
interpolates datapoints along the curve according to HatchSeparation
Scales everything such that is independent of graph scale and terminal size (this, however, requires a dummy plot of the curves without hatch lines for getting the gnuplot variables GPVAL_X_MAX, GPVAL_X_MIN, GPVAL_TERM_XMAX, GPVAL_TERM_XMIN, GPVAL_Y_MAX, GPVAL_Y_MIN, GPVAL_TERM_YMAX, GPVAL_TERM_YMIN.
Limitations:
does not work (yet) with logarithmic axes
several independent paths need to be separated by two empty lines
if you are using it together with your contour lines you have to make sure that the contour line datapoints are in the right order (see comment in my first answer). Furthermore, gnuplot might generate contour lines which are separated with a single empty line. I can address this if I find some time or/and somebody requests it.
Edit: (completely revised version)
The previous script (to my opinion) was pretty messy and difficult to follow (although nobody complained ;-). I removed the calls to subprocedures and hence the prefixes for variables in the subprocedures and put all in one script, except the test data generation.
Have fun with hatching your lines! Comments and improvements are welcome!
Test data generation: SO57118566_createTestData.gp
### Create some circle test data
FILE = "SO57118566.dat"
set angle degrees
# create some test data
# x y r a0 a1 N
$myCircleParams <<EOD
1.0 0.3 0.6 0 360 120
2.4 0.3 0.6 0 360 120
3.8 0.3 0.6 0 360 120
1.7 -0.3 0.6 0 360 120
3.1 -0.3 0.6 0 360 120
EOD
X(n) = real(word($myCircleParams[n],1)) # center x
Y(n) = real(word($myCircleParams[n],2)) # center y
R(n) = real(word($myCircleParams[n],3)) # radius
A0(n) = real(word($myCircleParams[n],4)) # start angle
A1(n) = real(word($myCircleParams[n],5)) # end angle
N(n) = int(word($myCircleParams[n],6)) # number of samples
set table FILE
do for [i=1:|$myCircleParams|] {
set samples N(i)
plot [A0(i):A1(i)] '+' u (X(i)+R(i)*cos($1)):(Y(i)+R(i)*sin($1))
}
unset table
set size ratio -1
plot FILE u 1:2:-2 w l lc var
### end of script
Strange enough, the previous version worked for gnuplot5.2.0 to 5.2.7 but not for gnuplot>=5.2.8. With this current script it is vice versa, but I haven't yet found out why.
Update:
Finally found why it wasn't working with <=5.2.7. Apparently something with the scaling which has changed between 5.2.7 and 5.2.8. Other terminals than wxt or qt might have different scaling factors.
You need to add/change the lines (already added in the script below):
Factor = GPVAL_VERSION==5.2 && int(GPVAL_PATCHLEVEL)<=7 ? \
GPVAL_TERM eq "wxt" ? 20 : GPVAL_TERM eq "qt" ? 10 : 1 : 1
Rxaupu = (GPVAL_X_MAX-xmin)/(GPVAL_TERM_XMAX-xtmin)*Factor # x ratio axes units to pixel units
Ryaupu = (GPVAL_Y_MAX-ymin)/(GPVAL_TERM_YMAX-ytmin)*Factor # y
Script: (tested with gnuplot 5.2.0, 5.2.7, 5.2.8, 5.4.1)
### Add hatch pattern to a curve
reset session
FILE = "SO57118566.dat"
set size ratio -1 # set same x,y scaling
set angle degree
unset key
# plot path without hatch lines to get the proper gnuplot variables: GPVAL_...
plot FILE u 1:2:-2 w l lc var
# Hatch parameters:
# TiltAngle >0°: left side, <0° right side in direction of path
# HatchLength hatch line length in pixels
# HatchGap separation of hatch lines in pixels
# TA HL HG Color
$myHatchParams <<EOD
-90 10 5 0x0080ff
-30 15 10 0x000000
90 5 3 0xff0000
45 25 12 0xffff00
-60 10 7 0x00c000
EOD
# extract hatch parameters
TA(n) = real(word($myHatchParams[n],1)) # TiltAngle
HL(n) = real(word($myHatchParams[n],2)) # HatchLength
Gpx(n) = real(word($myHatchParams[n],3)) # HatchGap in pixels
Color(n) = int(word($myHatchParams[n],4)) # Color
# terminal constants
xmin = GPVAL_X_MIN
ymin = GPVAL_Y_MIN
xtmin = GPVAL_TERM_XMIN
ytmin = GPVAL_TERM_YMIN
Factor = GPVAL_VERSION==5.2 && int(GPVAL_PATCHLEVEL)<=7 ? \
GPVAL_TERM eq "wxt" ? 20 : GPVAL_TERM eq "qt" ? 10 : 1 : 1
Rxaupu = (GPVAL_X_MAX-xmin)/(GPVAL_TERM_XMAX-xtmin)*Factor # x ratio axes units to pixel units
Ryaupu = (GPVAL_Y_MAX-ymin)/(GPVAL_TERM_YMAX-ytmin)*Factor # y
Angle(dx,dy) = dx==0 && dy==0 ? NaN : atan2(dy,dx) # -180° to +180°, NaN if dx,dy==0
LP(dx,dy) = sqrt(dx**2 + dy**2) # length of path segment
ax2px(x) = (x-xmin)/Rxaupu + xtmin # x axes coordinates to pixel coordinates
ay2py(y) = (y-ymin)/Rxaupu + ytmin # y
px2ax(x) = (x-xtmin)*Rxaupu + xmin # x pixel coordinates to axes coordinates
py2ay(y) = (y-ytmin)*Rxaupu + ymin # y
# create datablock $Path with pixel coordinates and cumulated path length
stats FILE u 0 nooutput # get number of blocks of input file
N = STATS_blocks
set table $Path
do for [i=0:N-1] {
x1 = y1 = NaN
Length = 0
plot FILE u (x0=x1, x1=ax2px($1)):(y0=y1, y1=ay2py($2)): \
(dx=x1-x0, dy=y1-y0, ($0>0?Length=Length+LP(dx,dy):Length)) index i w table
plot '+' u ('') every ::0::1 w table # two empty lines
}
unset table
# create hatch lines table
# resample data in equidistant steps along the length of the path
$Temp <<EOD # datablock $Temp definition required for function definition below
EOD
x0(n) = real(word($Temp[n],1)) # x coordinate
y0(n) = real(word($Temp[n],2)) # y coordinate
r0(n) = real(word($Temp[n],3)) # cumulated path length
ap(n) = Angle(x0(n+1)-x0(n),y0(n+1)-y0(n)) # path angle
ah(n,i) = ap(n)+TA(i+1) # hatch line angle
Frac(n) = (ri-r0(n))/(r0(n+1)-r0(n)) # interpolation along
hsx(n) = (x0(n) + Frac(n)*(x0(n+1)-x0(n))) # x hatch line start point
hsy(n) = (y0(n) + Frac(n)*(y0(n+1)-y0(n))) # y
hex(n,i) = (hsx(n) + HL(i+1)*cos(ah(n,i))) # x hatch line end point
hey(n,i) = (hsy(n) + HL(i+1)*sin(ah(n,i))) # y
# create datblock with hatchlines x,y,dx,dy
set print $HatchLines
do for [i=0:N-1] {
set table $Temp
splot $Path u 1:2:3 index i
unset table
ri = -Gpx(i+1)
do for [j=1:|$Temp|-2] {
if (strlen($Temp[j])==0 || $Temp[j][1:1] eq '#') {print $Temp[j]}
else {
while (ri<r0(j)) {
ri = ri + Gpx(i+1)
print sprintf("%g %g %g %g", \
xs=px2ax(hsx(j)), ys=py2ay(hsy(j)), \
px2ax(hex(j,i))-xs, py2ay(hey(j,i))-ys)
}
}
}
print ""; print "" # two empty lines
}
set print
plot $Path u (px2ax($1)):(py2ay($2)):(Color(column(-2)+1)) w l lc rgb var, \
$HatchLines u 1:2:3:4:(Color(column(-2)+1)) w vec nohead lc rgb var
### end of script
Result:
If you really want to have good hatch marks, you can draw a whole lot of arrows with no heads.
The example below computes the locations and slopes of each hatch mark in the loop making them nearly perpendicular to the drawn line (to numerical accuracy). It also spaces them along the line (again to rudimentary numerical accuracy but for a plot it is more than good enough.
reset
set grid
set sample 1000
set xrange [0:6]
set yrange [0:6]
# First, plot the actual curve
plot 1/log(x)
# Choose a length for your hatch marks, this will
# depend on your axis scale.
Hlength = 0.2
# Choose a distance along the curve for the hatch marks. Again
# will depend on you axis scale.
Hspace = 0.5
# Identify one end of the curve on the plot, set x location for
# first hatch mark.
# For this case, it is when 1/log(x) = 4
x1point = exp(0.25)
y1point = 1/log(x1point)
# Its just easier to guess how many hatch marks you need instead
# of trying to compute the length of the line.
do for [loop=1:14] {
# Next, find the slope of the function at this point.
# If you have the exact derivative, use that.
# This example assumes you perhaps have a user defined funtion
# that is likely too difficult to get a derivative so it
# increments x by a small amount to numerically compute it
slope = (1/log(x1point+0.001)-y1point)/(0.001)
#slopeAng = atan2(slope)
slopeAng = atan2((1/log(x1point+.001)-y1point),0.001)
# Also find the perpendicular to this slope
perp = 1/slope
# Get angle of perp from horizontal
perpAng = atan(perp)
# Draw a small hatch mark at this point
x2point = x1point + Hlength*cos(perpAng)
y2point = y1point - Hlength*sin(perpAng)
# The hatch mark is just an arrow with no heads
set arrow from x1point,y1point to x2point,y2point nohead
# Move along the curve approximately a distance of Hspace
x1point = x1point + Hspace*cos(slopeAng)
y1point = 1/log(x1point)
# loop around to do next hatch mark
}
replot
You will get something like this
Note that you can easily adjust the hatch mark length and the spacing between them. Also, if your x and y axis have significantly different scales, it would not be too hard to scale the x or y length of the arrow so they 'look' like equal lengths.
Edit:
You have the added complication of doing a contour plot. I've completed what you need to do. I resolved your g1 and g2 functions at the contour level you wanted the constraints and named two new functions g1_26 and g2_20 and solved for y for each.
I also discoverd that the hatch marks change sides with the simple program above when the sign of the slope changes so I added the sgn(slope) when calculating the x2 and y2 points of the hatch mark and also added a flip variable so you can easily control which side of the line the hatch marks are drawn. Here is the code:
### contour lines with labels
reset session
f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
g1(x,y)=(x-5)**2+y**2
g2(x,y) = 4*x+y
set xrange [0:6]
set yrange [0:6]
set isosample 250, 250
set key outside
set contour base
set cntrparam levels disc 10,30,75,150,300,500,850,1500
unset surface
set table $Contourf
splot f(x,y)
unset table
set contour base
set cntrparam levels disc 26
unset surface
set table $Contourg1
splot g1(x,y)
unset table
set contour base
set cntrparam levels disc 20
unset surface
set table $Contourg2
splot g2(x,y)
unset table
set style textbox opaque noborder
set datafile commentschar " "
plot for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5)
replot $Contourg1 u 1:2:(1) skip 5 index 0 w l lw 4 lc 0 title columnheader(5)
replot $Contourg2 u 1:2:(1) skip 5 index 0 w l lw 4 lc 0 title columnheader(5)
###############################
# Flip should be -1 or 1 depending on which side you want hatched.
flip = -1
# put hatches on g1
# Since your g1 constraint is at g1(x,y) = 26, lets
# get new formula for this specific line.
#g1(x,y)=(x-5)**2+y**2 = 26
g1_26(x) = sqrt( -(x-5)**2 + 26)
# Choose a length for your hatch marks, this will
# depend on your axis scale.
Hlength = 0.15
# Choose a distance along the curve for the hatch marks. Again
# will depend on you axis scale.
Hspace = 0.2
# Identify one end of the curve on the plot, set x location for
# first hatch mark.
x1point = 0
y1point = g1_26(x1point)
# Its just easier to guess how many hatch marks you need instead
# of trying to compute the length of the line.
do for [loop=1:41] {
# Next, find the slope of the function at this point.
# If you have the exact derivative, use that.
# This example assumes you perhaps have a user defined funtion
# that is likely too difficult to get a derivative so it
# increments x by a small amount to numerically compute it
slope = (g1_26(x1point+0.001)-y1point)/(0.001)
#slopeAng = atan2(slope)
slopeAng = atan2((g1_26(x1point+.001)-y1point),0.001)
# Also find the perpendicular to this slope
perp = 1/slope
# Get angle of perp from horizontal
perpAng = atan(perp)
# Draw a small hatch mark at this point
x2point = x1point + flip*sgn(slope)*Hlength*cos(perpAng)
y2point = y1point - flip*sgn(slope)*Hlength*sin(perpAng)
# The hatch mark is just an arrow with no heads
set arrow from x1point,y1point to x2point,y2point nohead lw 2
# Move along the curve approximately a distance of Hspace
x1point = x1point + Hspace*cos(slopeAng)
y1point = g1_26(x1point)
# loop around to do next hatch mark
}
###############################
# Flip should be -1 or 1 depending on which side you want hatched.
flip = -1
# put hatches on g2
# Since your g2 constraint is at g2(x,y) = 20, lets
# get new formula for this specific line.
#g2(x,y) = 4*x+y = 20
g2_20(x) = 20 - 4*x
# Choose a length for your hatch marks, this will
# depend on your axis scale.
Hlength = 0.15
# Choose a distance along the curve for the hatch marks. Again
# will depend on you axis scale.
Hspace = 0.2
# Identify one end of the curve on the plot, set x location for
# first hatch mark.
x1point =3.5
y1point = g2_20(x1point)
# Its just easier to guess how many hatch marks you need instead
# of trying to compute the length of the line.
do for [loop=1:32] {
# Next, find the slope of the function at this point.
# If you have the exact derivative, use that.
# This example assumes you perhaps have a user defined funtion
# that is likely too difficult to get a derivative so it
# increments x by a small amount to numerically compute it
slope = (g2_20(x1point+0.001)-y1point)/(0.001)
slopeAng = atan2((g2_20(x1point+.001)-y1point),0.001)
# Also find the perpendicular to this slope
perp = 1/slope
# Get angle of perp from horizontal
perpAng = atan(perp)
# Draw a small hatch mark at this point
x2point = x1point + flip*sgn(slope)*Hlength*cos(perpAng)
y2point = y1point - flip*sgn(slope)*Hlength*sin(perpAng)
# The hatch mark is just an arrow with no heads
set arrow from x1point,y1point to x2point,y2point nohead lw 2
# Move along the curve approximately a distance of Hspace
x1point = x1point + Hspace*cos(slopeAng)
y1point = g2_20(x1point)
# loop around to do next hatch mark
}
replot
Here is the result:
Comment: forget about these early cumbersome attempts. Nevertheless, I will leave it here. Please check my other answer.
I'm not aware of a feature in gnuplot which would generate such hatched lines.
One workaround could be the following: shift your curves slightly by some value and fill it with filledcurves and a hatch pattern. However, this works only well if the curve is a straight line or not too much bent.
Unfortunately, there is also only a very limited number of hatch patterns in gnuplot (see Hatch patterns in gnuplot) and they are not customizable.
You need to play with the shift value and the hatched fill pattern.
Code:
### contour lines with hatched side
reset session
f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
g1(x,y)=(x-5)**2+y**2
g2(x,y) = 4*x+y
set xrange [0:6]
set yrange [0:6]
set isosample 250, 250
set key outside
set contour base
unset surface
set cntrparam levels disc 10,30,75,150,300,500,850,1500
set table $Contourf
splot f(x,y)
unset table
set cntrparam levels disc 26
set table $Contourg1
splot g1(x,y)
unset table
set cntrparam levels disc 20
set table $Contourg2
splot g2(x,y)
unset table
set angle degree
set datafile commentschar " "
plot for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5)
replot $Contourg1 u 1:2 skip 5 index 0 w l lw 4 lc 0 title columnheader(5)
replot $Contourg2 u 1:2 skip 5 index 0 w l lw 4 lc 0 title columnheader(5)
set style fill transparent pattern 5
replot $Contourg1 u 1:2:($2+0.2) skip 5 index 0 w filledcurves lc 0 notitle
set style fill transparent pattern 4
replot $Contourg2 u 1:2:($2+0.5) skip 5 index 0 w filledcurves lc 0 notitle
### end of code
Result:
Addition:
With gnuplot you will probably find a workaround most of the times. It's just a matter how complicated or ugly you allow it to become.
For such steep functions use the following "trick". The basic idea is simple: take the original curve and the shifted one and combine these two curves and plot them as filled. But you have to reverse one of the curves (similar to what I already described earlier: https://stackoverflow.com/a/53769446/7295599).
However, here, a new "problem" arises. For whatever reason, the contour line data consist out of several blocks separated by an empty line and it's not a continous sequence in x. I don't know why but that's the contour lines gnuplot creates. To get the order right, plot the data into a new datablock $ContourgOnePiece starting from the last block (every :::N::N)to the first block (every :::0::0). Determine the number of these "blocks" by stats $Contourg and STATS_blank. Do the same thing for the shifted contour line into $ContourgShiftedOnePiece.
Then combine the two datablocks by printing them line by line to a new datablock $ClosedCurveHatchArea, where you actually reverse one of them.
This procedure will work OK for strictly monotonous curves, but I guess you will get problems with oscillating or closed curves. But I guess there might be also some other weird workarounds.
I admit, this is not a "clean" and "robust" solution, but it somehow works.
Code:
### lines with one hatched side
reset session
set size square
g(x,y) = -0.8-1/x**3+y
set xrange [0:4]
set yrange [0:4]
set isosample 250, 250
set key off
set contour base
unset surface
set cntrparam levels disc 0
set table $Contourg
splot g(x,y)
unset table
set angle degree
set datafile commentschar " "
# determine how many pieces $Contourg has
stats $Contourg skip 6 nooutput # skip 6 lines
N = STATS_blank-1 # number of empty lines
set table $ContourgOnePiece
do for [i=N:0:-1] {
plot $Contourg u 1:2 skip 5 index 0 every :::i::i with table
}
unset table
# do the same thing with the shifted $Contourg
set table $ContourgShiftedOnePiece
do for [i=N:0:-1] {
plot $Contourg u ($1+0.1):($2+0.1):2 skip 5 index 0 every :::i::i with table
}
unset table
# add the two curves but reverse the second of them
set print $ClosedCurveHatchArea append
do for [i=1:|$ContourgOnePiece|:1] {
print $ContourgOnePiece[i]
}
do for [i=|$ContourgShiftedOnePiece|:1:-1] {
print $ContourgShiftedOnePiece[i]
}
set print
plot $Contourg u 1:2 skip 5 index 0 w l lw 2 lc 0 title columnheader(5)
set style fill transparent pattern 5 noborder
replot $ClosedCurveHatchArea u 1:2 w filledcurves lc 0
### end of code
Result:
Addition 2:
Actually, I like #Ethan's approach of creating an extra level contour line. This works well as long as the gradient is not too large. Otherwise you might get noticeable deformations of the second contour line (see red curve below). However, in the above examples with g1 and g2 you won't notice a difference. Another advantages is that the hatch lines are perpendicular to the curve. A disadvantage is that you might get some interruptions of the regular pattern.
The solution with a small shift of the original curve in x and/or y and filling areas doesn't work with oscillating or closed lines.
Below, the black hatched curves are a mix of these approaches.
Procedure:
create a single contour line
create an extended (ext) or shifted (shf) contourline (either by a new contour value or by shifting an existing one)
order the contour line (ord)
reverse the contour lin (rev)
add the ordered (ord) and the extended,ordered,reversed (extordrev)
plot the added contour line (add) with filledcuves
NB: if you want to shift a contour line by x,y you have to order first and then shift it, otherwise the macro #ContourOrder cannot order it anymore.
You see, it can get complicated. In summary, so far there are three approaches:
(a) extra level contour line and thick dashed line (#Ethan)
pro: short, works for oscillating and closed curves;
con: bad if large gradient
(b) x,y shifted contour line and hatched filledcurves (#theozh)
pro: few parameters, clear picture;
con: lengthy, only 4 hatch patterns)
(c) derivative of data point (#Dan Sp.)
pro: possibly flexibility for tilted hatch patterns;
con: need of derivative (numerical if no function but datapoints), pattern depends on scale
The black curves are actually a mix of (a) and (b).
The blue curve is (b). Neither (a) nor (b) will work nicely on the red curve. Maybe (c)?
You could think of further mixing the approaches... but this probably gets also lengthy.
Code:
### contour lines with hashed side
set term wxt butt
reset session
f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
g1(x,y)=(x-5)**2+y**2
g2(x,y) = 4*x+y
g3(x,y) = -0.8-1/x**3+y
set xrange [0:6]
set yrange [0:6]
set isosample 250, 250
set key outside
set contour base
unset surface
set cntrparam levels disc 10,30,75,150,300,500,850,1500
set table $Contourf
splot f(x,y)
unset table
set cntrparam levels disc 26
set table $Contourg1
splot g1(x,y)
unset table
set cntrparam levels disc 20
set table $Contourg2
splot g2(x,y)
unset table
set cntrparam levels disc 0
set table $Contourg3
splot g3(x,y)
unset table
# create some extra offset contour lines
# macro for setting contour lines
ContourCreate = '\
set cntrparam levels disc Level; \
set table #Output; \
splot #Input; \
unset table'
Level = 27.5
Input = 'g1(x,y)'
Output = '$Contourg1_ext'
#ContourCreate
Level = 20.5
Input = 'g2(x,y)'
Output = '$Contourg2_ext'
#ContourCreate
Level = 10
Input = 'f(x,y)'
Output = '$Contourf0'
#ContourCreate
Level = 13
Input = 'f(x,y)'
Output = '$Contourf0_ext'
#ContourCreate
# Macro for ordering the datapoints of the contour lines which might be split
ContourOrder = '\
stats #DataIn skip 6 nooutput; \
N = STATS_blank-1; \
set table #DataOut; \
do for [i=N:0:-1] { plot #DataIn u 1:2 skip 5 index 0 every :::i::i with table }; \
unset table'
DataIn = '$Contourg1'
DataOut = '$Contourg1_ord'
#ContourOrder
DataIn = '$Contourg1_ext'
DataOut = '$Contourg1_extord'
#ContourOrder
DataIn = '$Contourg2'
DataOut = '$Contourg2_ord'
#ContourOrder
DataIn = '$Contourg2_ext'
DataOut = '$Contourg2_extord'
#ContourOrder
DataIn = '$Contourg3'
DataOut = '$Contourg3_ord'
#ContourOrder
set table $Contourg3_ordshf
plot $Contourg3_ord u ($1+0.15):($2+0.15) w table # shift the curve
unset table
DataIn = '$Contourf0'
DataOut = '$Contourf0_ord'
#ContourOrder
DataIn = '$Contourf0_ext'
DataOut = '$Contourf0_extord'
#ContourOrder
# Macro for reversing a datablock
ContourReverse = '\
set print #DataOut; \
do for [i=|#DataIn|:1:-1] { print #DataIn[i]}; \
set print'
DataIn = '$Contourg1_extord'
DataOut = '$Contourg1_extordrev'
#ContourReverse
DataIn = '$Contourg2_extord'
DataOut = '$Contourg2_extordrev'
#ContourReverse
DataIn = '$Contourg3_ordshf'
DataOut = '$Contourg3_ordshfrev'
#ContourReverse
DataIn = '$Contourf0_extord'
DataOut = '$Contourf0_extordrev'
#ContourReverse
# Macro for adding datablocks
ContourAdd = '\
set print #DataOut; \
do for [i=|#DataIn1|:1:-1] { print #DataIn1[i]}; \
do for [i=|#DataIn2|:1:-1] { print #DataIn2[i]}; \
set print'
DataIn1 = '$Contourg1_ord'
DataIn2 = '$Contourg1_extordrev'
DataOut = '$Contourg1_add'
#ContourAdd
DataIn1 = '$Contourg2_ord'
DataIn2 = '$Contourg2_extordrev'
DataOut = '$Contourg2_add'
#ContourAdd
DataIn1 = '$Contourg3_ord'
DataIn2 = '$Contourg3_ordshfrev'
DataOut = '$Contourg3_add'
#ContourAdd
DataIn1 = '$Contourf0_ord'
DataIn2 = '$Contourf0_extordrev'
DataOut = '$Contourf0_add'
#ContourAdd
set style fill noborder
set datafile commentschar " "
plot \
for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5), \
$Contourg1 u 1:2 skip 5 index 0 w l lw 3 lc 0 title columnheader(5), \
$Contourg2 u 1:2 skip 5 index 0 w l lw 3 lc 0 title columnheader(5), \
$Contourg3 u 1:2 skip 5 index 0 w l lw 3 lc 0 title columnheader(5), \
$Contourg1_add u 1:2 w filledcurves fs transparent pattern 4 lc rgb "black" notitle, \
$Contourg2_add u 1:2 w filledcurves fs transparent pattern 5 lc rgb "black" notitle, \
$Contourg3_add u 1:2 w filledcurves fs transparent pattern 5 lc rgb "blue" notitle, \
$Contourf0_add u 1:2 w filledcurves fs transparent pattern 6 lc rgb "red" notitle, \
### end of code
Result:
Addition 3:
If you plot a line with filledcurves, I guess gnuplot will connect the first and last point with a straight line and fills the enclosed area.
In your circle/ellipse example the outer curve is cut at the top border of the graph. I guess that's why the script does not work in this case. You have to identify these points where the outer curve starts and ends and arrange your connected curve such that these points will be the start and end point.
You see it's getting complicated...
The following should illustrate how it should work: make one curve where you start e.g. with the inner curve from point 1 to 100, then add point 1 of inner curve again, continue with point 1 of outer curve (which has opposite direction) to point 100 and add point 1 of outer curve again. Then gnuplot will close the curve by connecting point 1 of outer curve with point 1 of inner curve. Then plot it as filled with hatch pattern.
By the way, if you change your function g1(x,y) to g1(x,y)= x*y/2+(x+2)**2+(y-1.5)**2/2-2
(note the difference y-1.5 instead of y-2) everything works fine. See below.
Code:
### Hatching on a closed line
reset session
f(x,y)=x*exp(-x**2-y**2)+(x**2+y**2)/20
g1(x,y)= x*y/2+(x+2)**2+(y-1.5)**2/2-2
set xrange [-7:7]
set yrange [-7:7]
set isosample 250, 250
set key outside
set contour base
unset surface
set cntrparam levels disc 4,3.5,3,2.5,2,1.5,1,0.5,0
set table $Contourf
splot f(x,y)
unset table
set cntrparam levels disc 0
set table $Contourg1
splot g1(x,y)
unset table
# create some extra offset contour lines
# macro for setting contour lines
ContourCreate = '\
set cntrparam levels disc Level; \
set table #Output; \
splot #Input; \
unset table'
Level = 1
Input = 'g1(x,y)'
Output = '$Contourg1_ext'
#ContourCreate
# Macro for ordering the datapoints of the contour lines which might be split
ContourOrder = '\
stats #DataIn skip 6 nooutput; \
N = STATS_blank-1; \
set table #DataOut; \
do for [i=N:0:-1] { plot #DataIn u 1:2 skip 5 index 0 every :::i::i with table }; \
unset table'
DataIn = '$Contourg1'
DataOut = '$Contourg1_ord'
#ContourOrder
DataIn = '$Contourg1_ext'
DataOut = '$Contourg1_extord'
#ContourOrder
# Macro for reversing a datablock
ContourReverse = '\
set print #DataOut; \
do for [i=|#DataIn|:1:-1] { print #DataIn[i]}; \
set print'
DataIn = '$Contourg1_extord'
DataOut = '$Contourg1_extordrev'
#ContourReverse
# Macro for adding datablocks
ContourAdd = '\
set print #DataOut; \
do for [i=|#DataIn1|:1:-1] { print #DataIn1[i]}; \
do for [i=|#DataIn2|:1:-1] { print #DataIn2[i]}; \
set print'
DataIn2 = '$Contourg1_ord'
DataIn1 = '$Contourg1_extordrev'
DataOut = '$Contourg1_add'
#ContourAdd
set style fill noborder
set datafile commentschar " "
plot \
for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5), \
$Contourg1 u 1:2 skip 5 index 0 w l lw 2 lc 0 title columnheader(5), \
$Contourg1_add u 1:2 w filledcurves fs transparent pattern 5 lc rgb "black" notitle
### end of code
Result:
Late to the party, but this seems to still be of interest to some.
I'm the author of the Matlab implementation referenced by the OP.
https://www.mathworks.com/matlabcentral/fileexchange/29121-hatched-lines-and-contours
As it turns out, that version was not the first one I did (just the first to be shared online). The OG version is one I wrote for Java using Graphics2D:
https://github.com/ramcdona/HatchedStroke
I technically have a 3D version implemented in Java3D. If anyone needs it, let me know.
Most recently, I also implemented a version in Python in Matplotlib -- available since 3.4.0.
https://matplotlib.org/stable/gallery/images_contours_and_fields/contours_in_optimization_demo.html
It seems I need this every time I use another plotting tool. Python's Plotly is a likely next target. Maybe someone else will beat me to it.

Gnuplot: Scatter plot and density

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:
However I want something like that
Is that possible in Gnuplot? Any ideas?
(edit: revised and simplified)
Probably a much better way than my previous answer is the following:
For each data point check how many other data points are within a radius of R. You need to play with the value or R to get some reasonable graph.
Indexing the datalines requires gnuplot>=5.2.0 and the data in a datablock (without empty lines). You can either first plot your file into a datablock (check help table) or see here:
gnuplot: load datafile 1:1 into datablock
The time for creating this graph will increase with number of points O(N^2) because you have to check each point against all others. I'm not sure if there is a smarter and faster method. The example below with 1200 datapoints will take about 4 seconds on my laptop. You basically can apply the same principle for 3D.
Script: works with gnuplot>=5.2.0
### 2D density color plot
reset session
t1 = time(0.0)
# create some random rest data
set table $Data
set samples 700
plot '+' u (invnorm(rand(0))):(invnorm(rand(0))) w table
set samples 500
plot '+' u (invnorm(rand(0))+2):(invnorm(rand(0))+2) w table
unset table
print sprintf("Time data creation: %.3f s",(t0=t1,t1=time(0.0),t1-t0))
# for each datapoint: how many other datapoints are within radius R
R = 0.5 # Radius to check
Dist(x0,y0,x1,y1) = sqrt((x1-x0)**2 + (y1-y0)**2)
set print $Density
do for [i=1:|$Data|] {
x0 = real(word($Data[i],1))
y0 = real(word($Data[i],2))
c = 0
stats $Data u (Dist(x0,y0,$1,$2)<=R ? c=c+1 : 0) nooutput
d = c / (pi * R**2) # density: points per unit area
print sprintf("%g %g %d", x0, y0, d)
}
set print
print sprintf("Time density check: %.3f sec",(t0=t1,t1=time(0.0),t1-t0))
set size ratio -1 # same screen units for x and y
set palette rgb 33,13,10
plot $Density u 1:2:3 w p pt 7 lc palette z notitle
### end of script
Result:
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:
This is the result:
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:

Resources