Simple heaviside function graphic with gnuplot? - gnuplot

I basically want this (first diagram) done with gnuplot. I've searched and found nothing exactly like this. I can do a good heaviside without the little circles at the end and start of the two lines, but I can't seem to get it with the little circles. Actually, the second diagram would be nice to know too. The third too, but I'm not greedy.

Just for the records and completeness... although you can define a function
H(x) = x<0 ? 0 : 1
If you plot
plot H(x) w l
the line will be continuous at zero and of course without points.
So, another suggestion with just two columns x,y and variable pointtype would be the following.
Code:
### Heaviside function
reset session
$Heaviside <<EOD
-2 0
0 0
0 0.5
0 1
2 1
EOD
set yrange [-1:2]
set ytics 1
unset key
set multiplot layout 3,1
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:($0==1||$0==3?$2:NaN):($0==3?7:6) w p pt var lc 0
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:($0==1||$0==2||$0==3?$2:NaN):($0==2?7:6) w p pt var lc 0
set xrange [0:4]
a = 3
plot $Heaviside u ($1+a):2 w l lc 0, \
'' u ($1+a):($0==1||$0==3?$2:NaN):($0==3?7:6) w p pt var lc 0
unset multiplot
### end of code
Result:
Addition:
A variation with shorter and less confusing plot command, but using 4 columns together with variable pointtype. This will give the same result as above.
Code:
### Heaviside function
reset session
$Heaviside <<EOD
-2 0 NaN NaN
0 0 6 6
0 0.5 NaN 7
0 1 7 6
2 1 NaN NaN
EOD
set yrange [-1:2]
set ytics 1
unset key
set multiplot layout 3,1
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:2:3 w p pt var lc 0
plot $Heaviside u 1:2 w l lc 0, \
'' u 1:2:4 w p pt var lc 0
set xrange [0:4]
a = 3
plot $Heaviside u ($1+a):2 w l lc 0, \
'' u ($1+a):2:3 w p pt var lc 0
unset multiplot
### end of code
Addition 2:
In order to finalize the answer, here is an approach to plot functions containing the Heaviside function.
Instead of plotting from a datablock with fixed x-values (as in the two examples above) it uses the current x-range. Note, for example the syntax plot '+' u 1:(sin($1)) is basically identical with plot sin(x).
Apparently, setting the line color via lc rgb -1 does not plot a line, which can be used here to interrupt the line. You may want to increase the samples, e.g. set samples 300 to avoid a gap between the points and the function.
Code:
### plotting Heaviside function and functions containing Heaviside function
# including line interruption and inclusion/exclusion points
reset session
Heaviside(x,a) = x<a ? 0 : 1 # definition of Heaviside function
array Hpoints[2] = [6,7] # array for plotting "Heaviside points"
Hcolor(x) = (x0=x1, x1=x, x0<a && x1>=a ? -1 : 0xff0000) # set color -1 for line interruption
dx(n) = 1e-3*(2*n-1) # small dx to get y-value of points close to break
f(x,a) = 50/(x**2+2)*cos(4*x) * Heaviside(x,a)
unset key
set multiplot layout 2,1
a = 2.0
set yrange[-1:2]
plot x1=NaN '+' u 1:(Heaviside(x,a)):(Hcolor(x)) w l lc rgb var, \
Hpoints u (a):(Heaviside($1,a)):(Hpoints[$0+1]) w p pt var lc rgb Hcolor(NaN)
a= 0
set samples 300
set yrange[-25:35]
plot x1=NaN '+' u 1:(f(x,a)):(Hcolor(x)) w l lc rgb var, \
Hpoints u (a):(f(a+dx($0),a)):(Hpoints[$0+1]) w p pt var lc rgb Hcolor(NaN)
unset multiplot
### end of code
Result:

I created the following datafile (mind the two empty lines):
-2 0 0 1
0 0 2 1
0 0 0 1
and ran the following gnuplot commands:
set yrange [-2:2]
plot "file" using 1:2 with lines,\
"" using 3:4 with lines, \
"" index 1 using 1:2 with points pointtype 6, \
"" index 1 using 3:4 with points pointtype 7
Fix the colours to your liking.

Related

GNUPLOT draws step plot not within border restrictions

Drawing a step plot leads always to an out of border result.
How to solve the issue? Any ideas? THX!
The MWE is:
reset;set term png small size 500,500;set output 'test.png';
set title 'First step is always drawn out of chart borders ?!?';
unset y2tics;set y2range [0:40];set y2tics 10;set yrange [0:40];set ytics 10 mirror;
set style fill solid 1.00 border;
plot 'test.data' using 1:2 notitle with fillsteps lc rgb 'light-goldenrod', \
'' using 1:3 notitle with fillsteps lc rgb 'gray40', \
'' using 1:4 notitle with fillsteps lc rgb 'web-green', \
'' using 1:5 notitle with fillsteps lc rgb 'light-green';
The result is:
Used software is:
GNUPLOT Version 5.2 patchlevel 8
Ok, now I see your point. Looks like a little bug (or our limited understanding).
I cannot tell right away why this is, but you can avoid it
by adding a line in the beginning which contains the first x value and all y-values are 0.
If you don't want to do this manually, there would be ways to do this automatically with gnuplot.
But I hope there is a simpler solution.
Code:
### plot with fillsteps
reset session
$Data <<EOD
1 0 0 0 0
1 50 35 30 5
2 55 30 20 5
17 51 44 30 12
20 1 1 1 1
EOD
unset y2tics;set y2range [0:40]
set y2tics 10
set yrange [0:40]
set ytics 10 mirror
set style fill solid 1.00 border
unset key
plot $Data u 1:2 w fillsteps lc 'light-goldenrod', \
'' u 1:3 w fillsteps lc 'gray40', \
'' u 1:4 w fillsteps lc 'web-green', \
'' u 1:5 w fillsteps lc 'light-green'
### end of code
Result:
Addition: (automatically duplicate first line, to workaround the bug(!?))
In order to workaround this (what I would call unexpected or a bug) you want to duplicate the first line automatically. There would be certainly different easy ways with external tools, however, which would not guarantee platform-independence. So, here is one of several possible gnuplot-only solutions.
get your file into a datablock (here: $Data) (see gnuplot: load datafile 1:1 into datablock)
print the first line of $Data into a new datablock (here: $Data2) Make sure that the first line is not a header or commented line, i.e. print the first dataline.
append the full datablock $Data again to $Data2.
Data: (Test.dat)
1 50 35 30 5
2 55 30 20 5
17 51 44 30 12
20 1 1 1 1
Code: (Result same as above)
# https://stackoverflow.com/a/67151340/7295599
### plot with filledcurves
reset session
FileToDatablock(f,d) = GPVAL_SYSNAME[1:7] eq "Windows" ? \
sprintf('< echo %s ^<^<EOD & type "%s"',d,f) : \
sprintf('< echo "\%s <<EOD" & cat "%s"',d,f) # Linux/MacOS
FILE = 'Test.dat'
load FileToDatablock(FILE,'$Data')
set print $Data2
print $Data[1] # only first line
print $Data
set print
unset y2tics;set y2range [0:40]
set y2tics 10
set yrange [0:40]
set ytics 10 mirror
set style fill solid 1.00 border
unset key
plot $Data2 u 1:2 every ::0::0 w fillsteps lc 'light-goldenrod', \
'' u 1:2 w fillsteps lc 'light-goldenrod', \
'' u 1:3 w fillsteps lc 'gray40', \
'' u 1:4 w fillsteps lc 'web-green', \
'' u 1:5 w fillsteps lc 'light-green'
### end of code

Combining acspline with filledcurve on data

I know how to use filledcurve with using 1:2:3 to fill the area between the curves described by columns 2 and 3.
I also know how to smooth a single curve using acsplines with using 1:2:(0.1) (with 0.1 being the smoothing weight for each point).
But I fail to combine the two, i.e. fill the area between two smoothed curves. All variants I try give me error messages like duplicated or contradicting arguments in datafile options.
This is what I tried:
plot 'datafile' using 1:2:3:(0.1):(0.1) smooth acs with lines
plot 'datafile' using 1:2:(0.1):3:(0.1) smooth acs with lines
Is this combination possible at all? How is the syntax then?
I guess you can't combine smooth acspline and with filledcurves, but I also don't know why this shouldn't be possible.
Well, as a workaround, plot your smooth acspline curve into a data table first and then plot it with filledcurves.
Code:
### filled acspline curve
reset session
$Data <<EOD
1 2
2 4
3 3
4 1
EOD
# plot your acspline data into a table
set table $ACSpline
plot $Data u 1:2 smooth acspline
unset table
set style fill transparent solid 0.1
plot $Data u 1:2 w lp pt 7 lc rgb "black" ti "Original data", \
$Data u 1:2 smooth acspline lw 2 lc rgb "red" ti "acspline", \
$ACSpline u 1:2 w filledcurves x1 lc rgb "red" ti "acspline filled"
### end of code
Result:
Addition:
There is another way to get data into a table. Check help set print.
With this, you can get the data of the two splines into one datablock and can plot the area between them. Maybe someone knows a simpler way to achieve this.
Code:
### filled curve between two acsplines
reset session
$Data <<EOD
1 2 4
2 4 1
3 3 0
4 1 3
EOD
# plot your acspline data into tables
set table $ACSpline1
plot $Data u 1:2:(1) smooth acspline
unset table
set table $ACSpline2
plot $Data u 1:3:(1) smooth acspline
unset table
set print $BetweenSplines
do for [i=1:|$ACSpline1|] {
print sprintf("%s %s %s",word($ACSpline1[i],1),word($ACSpline1[i],2),word($ACSpline2[i],2))
}
set print
set style fill transparent solid 0.5
plot $Data u 1:2 w lp pt 7 lc rgb "black" ti "Original data col 2", \
$Data u 1:3 w lp pt 7 lc rgb "blue" ti "Original data col 3", \
$ACSpline1 u 1:2 w l lw 2 lc rgb "red" ti "acspline col 2", \
$ACSpline2 u 1:2 w l lw 2 lc rgb "green" ti "acspline col 3", \
$BetweenSplines u 1:2:3 w filledcurves lc rgb "yellow" ti "acspline filled"
### end of code
Result:

Separate key (legend) for colors and markers

I have a plot with several types of objects (each read from a separate file). I'm plotting the same several functions for all of them, all on the same graph (same X-axis).
I set the markers (pt) explicitly for each, and the color (lc), so the same object has the same marker, but the same function has the same color. As an example we have 2 files, one for each object (| is just to separate the files here):
0 0 0 | 0 1 1
1 1 2 | 1 1 2
Let's call the left file A, the right B. Column 1 in each file is the x axis, column 2 is using 1:2, and column 3 is using 1:3. So using the above files in an interactive session:
gnuplot> plot "A" using 1:2 with lp pt 1 lc 'black'
gnuplot> replot "A" using 1:3 with lp pt 1 lc 'red'
gnuplot> replot "B" using 1:2 with lp pt 2 lc 'black'
gnuplot> replot "B" using 1:3 with lp pt 2 lc 'red'
we get:
Is it possible to have the key separated, so A/B appear next to their respective marker, and the function name ("using...") appears next to a line (or anything) with the appropriate color?
Right now by omitting titles (notitle in the plot command) I can get one or the other, though I would have to settle on some uniform arbitrary marker/color (depending on what I chose to set as key). Can I:
Get two keys somehow? - Preferably setting the missing attribute (color or marker) to something not in the plot.
If not, can I customize a manual legend somehow?
I am not fully sure what you want to achieve, nevertheless as for the splitting of the key, I don't think that Gnuplot has some "out-of-the-box" feature for this. However, you could (ab)use multiplot to achieve this effect. The idea is basically to generate two overlapping plots - one with points and one with lines - and to position the keys independently:
set terminal pngcairo rounded font ",16"
set output 'fig.png'
$A << EOD
0 0 0
1 1 2
EOD
$B << EOD
0 1 1
1 1 2
EOD
set multiplot
set xtics out nomirror
set ytics out nomirror
eps = 0.1
set lmargin at screen eps
set rmargin at screen 1 - eps/2
set bmargin at screen eps
set tmargin at screen 1 - eps/2
#common key settings
set key left top Left reverse spacing 1.5
set key at screen 0.1,screen 1-eps
plot \
$A u 1:2 with p ps 1.5 pt 1 lc 'black' t 'A', \
$A u 1:3 with p ps 1.5 pt 1 lc 'red' t 'A' , \
$B u 1:2 with p ps 1.5 pt 2 lc 'black' t 'B', \
$B u 1:3 with p ps 1.5 pt 2 lc 'red' t 'B'
unset border; unset xtics; unset ytics
set key at screen 0.3,screen 1-eps
plot \
$A u 1:2 with l lc 'black' t 'using 1:2', \
$A u 1:3 with l lc 'red' t 'using 1:3', \
$B u 1:2 with l lc 'black' t '', \
$B u 1:3 with l lc 'red' t ''
This would give you:

gnuplot - calculate distance between lines

Can gnuplot calculate the distance between two lines or maybe two points?
I'm having a plot where two (main) lines are plotted. For the moment let's assume that the first line is always above the second one. Is there a way to calculate the distance from line 2 to line 1 at a given x-value?
here is a picture of what my plot looks like and which distance I want to calculate:
The vertical lines are just for style and have nothing to do with the actual plot, their data is stored in test.dat and test2.dat.
My data-files of the lines look like this:
line1
0 118.1
2.754 117.77
4.054 117.64
6.131 116.17
7.7 116.04
8.391 115.36
10.535 115.25
11.433 116.03
12.591 116.22
19.519 118.59
line2
19.4 118.51
15.2 116.56
10.9 115.94
10.35 114.93
9.05 114.92
8.3 115.9
5.9 116.19
4.2 116.62
2.2 117.66
-0.3 118.06
My plotting-code looks like this:
set term wxt enhanced
cd 'working directory'
unset key
set size 0.9,0.9
set origin 0.1,0.1
set title 'TITLE'
unset border
set label 21 " rotate by 45" at -3.0,0.0 rotate by 45 point ps 2
set xrange [0:19.519]
set yrange [110:119]
set xtics nomirror(0, 2.745, 4.054, 6.131, 7.7, 8.391, 10.535, 11.433, 12.591, 19.519) rotate by 90 offset 0,-0.1 right
set ytics " ", 30000
plot "line1.dat" using ($1):($2):2 with labels offset 1, 1.8 rotate by 90, "line1.dat" using 1:2 with lines lt 1 lw 1 lc rgb '#000000', +112 lt 1 lw 1 lc rgb '#000000' , 'test.dat' with lines lt 1 lw 1 lc rgb '#000000', +110 lt 1 lw 1 lc rgb '#000000', 'line2.dat' with lines lt 0.5 lw 1 lc rgb '#000000', 'test2.dat' with lines lt 0.5 lw 1 lc rgb '#000000'
You can measure the distance manually. Move the mouse to the first point and type 'r'. Then as you move the mouse around, the x and y offsets, distance and angle are displayed. Type '5' to draw a line segment and to toggle between degrees and tangent display. Zooming in beforehand increases accuracy.
By the way, typing 'h' in the plot window will display a list of keybindings to the console.
An answer to this "rather old" question still might be of interest to OP, if not, maybe to others.
Yes, you can calculate and plot the difference of two lines. It requires some linear interpolation. Simply assign the desired x-value to the variable myX.
Data:
SO17717287_1.dat
0 118.1
2.754 117.77
4.054 117.64
6.131 116.17
7.7 116.04
8.391 115.36
10.535 115.25
11.433 116.03
12.591 116.22
19.519 118.59
SO17717287_2.dat
19.4 118.51
15.2 116.56
10.9 115.94
10.35 114.93
9.05 114.92
8.3 115.9
5.9 116.19
4.2 116.62
2.2 117.66
-0.3 118.06
Script: (works for gnuplot>=4.6.0)
### calculating and plotting a difference between two curves
reset
FILE1 = "SO17717287_1.dat"
FILE2 = "SO17717287_2.dat"
set border 1
unset key
set origin 0.05,0.05
set size 0.9,0.8
set xrange [0:19.519]
set xtics nomirror rotate by 90 offset 0,-0.1 right
set yrange [110:119]
unset ytics
myX = 15.2
getYa(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? ya=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? ya=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
getYb(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? yb=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? yb=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
set samples 2 # set to minimal possible value for plotting '+'
plot x1=y1=NaN FILE1 u 1:2:2:xtic(1) w labels offset 0,0.5 left rotate by 90, \
'' u 1:(getYa(myX),$2) w l lc rgb 'black', \
'' u 1:2:(0):(110-$2) w vec lt 0 nohead, \
+112 w l lc rgb 'black', \
x1=y1=NaN FILE2 u 1:(getYb(myX),$2) w l lt 0 lc rgb 'black', \
'+' u (myX):(ya):(0):(yb-ya) w vec heads lc rgb "red", \
'+' u (myX):(ya):(sprintf("%.3f",yb-ya)):xtic(sprintf("%g",myX)) w labels tc rgb "red" offset 0,1, \
'+' u (myX):(ya):(0):(110-ya) w vec nohead lt 0 lc rgb "red"
### end of script
Result: (created with gnuplot 4.6.0)

Thicker lines in the legend of gnuplot

I'm plotting some data curves with gnuplot, and they look like this:
However, the line samples in the legend are too thin. When you have more curves, it becomes hard to distinguish the colors. You can increase the thickness of the curves using "linewidth", e.g., by adding "lw 3" to the plot command, and you'd get this:
However, this increases the thickness everywhere. Is it possible to make the lines thick in the legend only? I know it can be done "the other way", by postprocessing on the output .png file. But is there a direct approach, using some gnuplot setting/wizardry?
Unfortunately, I don't know a way to control the thickness of the lines in the key, since they correspond to the lines being drawn. You can see what you can change by typing help set key in gnuplot.
Using multiplot, you can draw the plot lines first without the key, then draw the key again for 'ghost lines'. Here is a code sample which would do that:
set terminal png color size 800,600
set output 'plot.png'
set multiplot
unset key
plot '../batteries/9v/carrefour.txt' w lp, \
'../batteries/9v/philips.txt' w lp, \
'../batteries/9v/sony.txt' w lp
set key; unset tics; unset border; unset xlabel; unset ylabel
plot [][0:1] 2 title 'Carrefour' lw 4, \
2 title 'Philips' lw 4, \
2 title 'Sony' lw 4
In the second plot command, the function 2 (a constant) is being plotted with a y range of 0 to 1, so it doesn't show up.
I ran across this post and it gave me a critical idea.
The provided solution does not work in multiplot mode, since the second plot command will trigger the second plot, which is most likely not desired.
as a workaround one can set the original data as "notitle", then plot data outside of range with the same linetype and color in different thickness with the desired title. I'll just leave my current example here. It also includes linestyles that i have declared. So i just use the same linestyle (ls) to get the same color but change the thickness on the second line.
# for pngs
set terminal pngcairo size 1600,600 font ',18' enhanced
set output "pic_multi_kenngr_ana.png
set style line 2 lc rgb '#0ce90b' lt 1 lw 1.5 # --- green
set style line 3 lc rgb '#09e0b3' lt 1 lw 1.5 # .
set style line 4 lc rgb '#065fd8' lt 1 lw 1.5 # .
set style line 5 lc rgb '#4e04cf' lt 1 lw 1.5 # .
set style line 6 lc rgb '#c702a9' lt 1 lw 1.5 # .
set style line 7 lc rgb '#bf000a' lt 1 lw 1.5 # --- red
set multiplot layout 1,2
set xtics rotate
set tmargin 5
set xtics 12
set grid xtics
# set axis labels
set ylabel 'T [K]'
set xlabel 'Zeit [h]'
# select range
set xrange [0:48]
set yrange [290.15:306.15]
set title "(a) Bodentemperatur"
set key top right Right
plot 'par_crank_hom01lvls.04.dat' u 1:3 with lines ls 7 notitle,\
'par_crank_str01lvls.16.dat' u 1:3 with lines ls 2 notitle,\
500 t 'z = 4 cm' ls 7 lw 4,\
500 t 'z = 16 cm' ls 2 lw 4
################################################
set title "(b) Bodenwärmestrom an der Oberfläche"
set ylabel 'G [W m^{-2}]'
set yrange[-110:110]
unset key
plot 'par_crank_str01_ghf.dat' u 1:3 with lines
unset multiplot
I hope this will help someone
An even more simple work-around (imho) is to define the colours explicitly and plot each line twice, once with high lw for the key and also with the title to appear in the key, but adding "every ::0::0" which effectively ends up in plotting nothing, and once the normal way. See the following code snippet:
plot data u 0:1 w l linecolor rgb #1b9e77 lw 2 t "",\
data every ::0::0 u 0:1 w l linecolor rgb #1b9e77 lw 4 t "Title"
To expand on the NaN comment by #Svalorzen, the following will graph two lines of width 1 from some datafile.txt with no titles and create matching blank lines with the specified titles and width 5 for the key only:
plot [][]\
NaN title "Title1" w line lt 1 lc 1 lw 5,\
NaN title "Title2" w line lt 1 lc 2 lw 5,\
"datafile.txt" using 1:2 title "" w line lt 1 lc 1 lw 1,\
"datafile.txt" using 1:3 title "" w line lt 1 lc 2 lw 1
I find an answer for this:
Set key linewidth
in your case, must be:
plot '../batteries/9v/carrefour.txt' w l lw 1 linetype 1 notitle, 0/0 linetype 1 linewidth 5 title 'Carrefour'
rep '../batteries/9v/philips.txt' w l lw 1 linetype 2 notitle, 0/0 linetype 2 linewidth 5 title 'Philips'
rep '../batteries/9v/sony.txt' w l lw 1, linetype 3 notitle, 0/0 linetype 3 linewidth 5 title 'Sony'
Try something like:
plot # ... \
keyentry w l lw 1 lc 2 t "Title" # ...
And remove the old keys.

Resources