GNUplot: string value in ternary operator - gnuplot

Coming back to an old project and have obviously forgotten something here.
Datafile:
2017-03-31 14:38 6.42 feet High Tide
2017-03-31 20:40 -0.30 feet Low Tide
2017-04-01 02:56 6.92 feet High Tide
2017-04-01 06:34 Full Moon
2017-04-01 09:19 -0.58 feet Low Tide
2017-04-01 15:33 6.17 feet High Tide
When I plot as follows, all works fine except the ternary operator to test for the string(s) in strcol(3):
set terminal aqua size 950,594
set timefmt "%Y-%m-%d %H:%M"
unset key
set samples 1000
set xtics 86400
set x2tics 86400
set xtics nomirror font "Gill Sans Light,14" tc rgb "blue"
set grid ytics back
set grid x2tics
set format x "%a"
set format x2 "%d"
set xdata time
set x2data time
set yrange [-1:7.5]
set xzeroaxis lt 1 lw 1 lc rgb "red"
myDate(col1,col2)=sprintf("%s %s",strcol(1),strcol(2))
myLabel(col3,col2) = sprintf("%s ft.\n%s",strcol(col3),strcol(col2))
plot "tidefiles-test2.txt" \
u (myDate(1,2)):3:((strcol(5) eq "High") ? (myLabel(3,2)) :1/0) with labels font "Gill Sans Light,12" offset 0,-2 tc rgb "blue",\
"" u (myDate(1,2)):3:((strcol(5) eq "Low") ? (myLabel(3,2)) :1/0) with labels font "Gill Sans Light,12" offset 0,+2 tc rgb "red",\
"" u (myDate(1,2)):3:(strcol(3) eq "Full") ? ("●"):1/0 w labels left font "Symbola,48" offset -4,0,\
"" u (myDate(1,2)):3 lc rgb "green" lw 1 sm cspl notitle

The line with the string Full doesn't have a y-value. So, when the condition strcol(3) eq "Full" is fulfilled, at the same time the actual y-value is not a valid number and so that point is also skipped.
Using e.g.
plot "tidefiles-test2.txt" \
"" u (myDate(1,2)):(strcol(3) eq "Full" ? 0 : $3):((strcol(3) eq "Full") ? ("●"):1/0) w labels left font "Symbola,24" offset -4,0
works fine, but requires you to use a hard-coded y-value. Otherwise you must add an appropriate y-value in your data file for the "Full"-case.

Related

Polar plot in gnuplot

I have a problem. Im trying to do a polar plot, the fact it's that i want to plot some theorical line as some experimental data in the plot. But the theorical line has a range different of the experimental data because i had to do it like that to had gnuplot paint it.
I have the following script:
####
reset
set encoding utf8
set size 1,1
set terminal epslatex
set output "direccionalidad.tex"
unset border
set polar
set angles degrees #set gnuplot on degrees instead of radians
set style line 10 lt 1 lc 0 lw 0.3
set grid polar 30 #set the grid to be displayed every 60 degrees
set grid ls 10
set trange[-90:90]
f(t)=27.934*sin(1.81651*t+96.1991) # Theorical line
set xrange[-31:32]
set yrange[-30:30]
set xtics axis #disply the xtics on the axis instead of on the border
set ytics axis
set xtics scale 0 #"remove" the tics so that only the y tics are displayedj
set ytics (0, 6, 12) #make the ytics go from the center (0) to 6000 with incrment of 1000
unset ytics
set xtics ("5" 6, "15" 16.5, "30" 32)
# set the xtics only go from 0 to 6000 with increment of1000 but do not display anything. This has to be done otherwise the grid will not be displayed correctly.
set rtics (5,15,30)
set rtics format ' ' scale 0
set_label(x, text) = sprintf("set label '%s' at (32*cos(%f)), (32*sin(%f)) center", text, x, x) #this places a label on the outside
eval set_label(0, "0")
eval set_label(30, "30")
eval set_label(60, "60")
eval set_label(90, "90")
eval set_label(120, "120")
eval set_label(150, "150")
eval set_label(180, "180")
eval set_label(-150, "-150")
eval set_label(-120, "-120")
eval set_label(-90, "-90")
eval set_label(-60, "-60")
eval set_label(-30, "-30")
set size square
#PLOTS
plot "direccionalidaddatos.txt" u 1:3 pointtype 7 ps 2 lt 1 lw 3 lc rgb 'blue' notitle ,\
f(t) dt '-' lc rgb 'blue' notitle
#ACABAMOS
###
And here there are some examples of columns in the direccionalidaddatos.txt archive, the column in the middle it's just the angle in radians but it's usless cause i just want the angle in degrees so just ignore it ;):
-90 -1.570796327 0.1
-85 -1.483529864 0.2
-80 -1.396263402 0.4
-75 -1.308996939 0.7
-70 -1.221730476 1.1
-65 -1.134464014 1.7
-60 -1.047197551 2.5
#
And the result it is the image Polar plot
As you see in the image there are two branches that have no points, i want those branches to simply don't disappear cause they don't mean notihng in the graphic.So if anyone knows a form to made dissapear the branches, or to improve the script let me know hehe.
Thanks so much.
Can you just reduce the sampled range for the second plot component?
plot "direccionalidaddatos.txt" u 1:3 pointtype 7 ps 2 lt 1 lw 3 lc rgb 'blue' notitle ,\
[-52:45] '+' using 1:(f($1)) with lines dt '-' lc rgb 'blue' notitle

How do I edit the time values on the x-axis to start from zero when using gnuplot?

I do not want to change my data files that come with the first column containing the time values. Then I formatted it on gnuplot to show only the hour and minute. But it is a bit ugly to start the time from 8:00. I would like to start it from 0 and keep the values at the same pace of the data file. I was trying to use a constant like this example shows How do I make a plot in gnuplot with the lowest value automatically subtracted from the y data? but it is not working.
Here are my source and the plot.
#!/usr/bin/gnuplot
# set grid
set key outside bottom center horizontal
set key font ",19"
set style line 1 lc rgb '#E02F44' lt 1 lw 1 ps 0.5 pt 7 # input throughput
set style line 2 lc rgb '#FF780A' lt 1 lw 1 ps 0.5 pt 1 # output throughput
set style line 3 lc rgb '#56A64B' lt 1 lw 1 ps 0.5 pt 2 # average processing latency
set style line 4 lc rgb '#000000' lt 1 lw 1 ps 0.5 pt 3 # 99th percentile processing latency
set style arrow 1 heads ls 4
set style arrow 2 head ls 4
set terminal pdf
set pointintervalbox 0
set datafile separator ','
set output "Cost-20K-ThroughputVsLatency.pdf"
#set title ""
set xlabel "time (minutes)" font ",17" offset 0,1,0
set xtics font ",8" offset 0,0.5,0
set xdata time # tells gnuplot the x axis is time data
set timefmt "%Y-%m-%d %H:%M:%S" # specify our time string format
set format x "%H:%M" # otherwise it will show only MM:SS
set xrange ["2020-05-07 08:05:00":"2020-05-07 09:50:00"]
set ylabel "Throughput (K rec/sec)" font ",18" offset 0,0,0
set yrange [0:7]
set ytics font ",20"
#set y2label "processing latency (seconds)" font ",18" offset -1.5,0,0
set y2range [0:25]
set ytics nomirror
set y2tics 0, 5 font ",17"
plot "throughput-vs-latency-20K.csv" using 1:(column(2)/1000) title "IN throughput" with linespoints ls 1 axis x1y1 \
, "throughput-vs-latency-20K.csv" using 1:(column(10)/1000) title "OUT throughput" with linespoints ls 2 axis x1y1 \
, "throughput-vs-latency-20K.csv" using 1:(column(18)/1000) title "avg. latency" with linespoints ls 3 axis x1y2 \
, "throughput-vs-latency-20K.csv" using 1:(column(26)/1000) title "99th latency" with linespoints ls 4 axis x1y2
UPDATE
I changed my script like you said #theozh but I am still not getting the x axis starting from 0.
set key bottom right
set key font ",11"
set style line 1 lc rgb '#E02F44' lt 1 lw 1 ps 0.5 pt 7 # input throughput
set style line 2 lc rgb '#FF780A' lt 1 lw 1 ps 0.5 pt 1 # output throughput
set style line 3 lc rgb '#56A64B' lt 1 lw 1 ps 0.5 pt 2 # average processing latency
set style line 4 lc rgb '#000000' lt 1 lw 1 ps 0.5 pt 3 # 99th percentile processing latency
set style arrow 1 heads ls 4
set term pdfcairo size 5.0in,2.5in
set pointintervalbox 0
set datafile separator ','
set tmargin 1.5
set border 1+2+8
set xtics nomirror
set output "throughput-latency-increasingK-TaxiRideNYC-50Kpersec.pdf"
myTimeFmt = "%Y-%m-%d %H:%M:%S"
set xlabel "time (minutes)" font ",9" offset 0,1.5,0
set xtics font ",8" #rotate by 45 right
set ylabel "Throughput (K rec/sec)" font ",10" offset 2,0,0
set yrange [0:3.5]
set y2label "processing latency (seconds)" font ",10" offset -2,0,0
set y2range [0:14]
set ytics nomirror
set y2tics 0, 2
set xdata time # tells gnuplot the x axis is time data
set format x "%M" time
plot t=0 "throughput-latency-increasing.csv" u (t==0?(t0=timecolumn(1,myTimeFmt),t=1):NaN, timecolumn(1,myTimeFmt)-t0):(column(2)/1000) title "IN throughput" with linespoints ls 1 axis x1y1 \
, t=0 "throughput-latency-increasing.csv" u (t==0?(t0=timecolumn(1,myTimeFmt),t=1):NaN, timecolumn(1,myTimeFmt)-t0):(column(18)/1000) title "avg. latency" with linespoints ls 3 axis x1y2 \
, 4/0 t "# of tuples pre-aggregating" with vectors arrowstyle 1
values are here:
"Time","pre_aggregate-outPool[0]-avg","pre_aggregate-outPool[1]-avg","pre_aggregate-outPool[2]-avg","pre_aggregate-outPool[3]-avg","pre_aggregate-outPool[4]-avg","pre_aggregate-outPool[5]-avg","pre_aggregate-outPool[6]-avg","pre_aggregate-outPool[7]-avg","pre_aggregate-outPool[0]-99","pre_aggregate-outPool[1]-99","pre_aggregate-outPool[2]-99","pre_aggregate-outPool[3]-99","pre_aggregate-outPool[4]-99","pre_aggregate-outPool[5]-99","pre_aggregate-outPool[6]-99","pre_aggregate-outPool[7]-99","pre_aggregate[0]-param","pre_aggregate[1]-param","pre_aggregate[2]-param","pre_aggregate[3]-param","pre_aggregate[4]-param","pre_aggregate[5]-param","pre_aggregate[6]-param","pre_aggregate[7]-param"
"2020-04-27 10:22:45",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33,70,75,79,33,41,62,75,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:23:00",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33,33,75,79,33,33,33,37,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:23:15",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33,33,33,33,33,33,33,33,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:23:30",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,62,66,50,62,66,45,50,66,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:23:45",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,62,66,50,62,66,45,50,66,50000,50000,50000,50000,50000,50000,50000,50000
"2020-04-27 10:24:00",33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33.33333432674408,33,33,33,33,33,33,33,33,50000,50000,50000,50000,50000,50000,50000,50000
The following example uses the newer gnuplot date time syntax (see help timecolumn), e.g. timecolumn(1,myTimeFmt) and set format x "%H:%M" time.
In order to normalize your time series to the first data point you have to store this time into a variable, e.g. t0 which you can "re-use" in successive plot commands from the same datafile.
Note the different time format for the x axis: "%H:%M" for day time and "%tH:%tM" for hours exceeding 24 hours or minutes exceeding 60 minutes, see help time_specifiers.
Edit:
for better readability of the plot command, I "outsourced" the normalization into a function Normalize(). But note that t=0 is still required at the beginning of the plot command.
in case you have some (uncommented) header lines, you need to skip them via skip <number of header lines>.
Code:
### normalize time data relative to start time
reset session
myTimeFmt = "%Y-%m-%d %H:%M:%S"
# create some test data
set table $Data
plot '+' u (strftime(myTimeFmt,time(0) + $1*3600*2)):(cos($1)) w table
unset table
# function to normalize time column to first value
Normalize(c) = (t==0?(t0=timecolumn(c,myTimeFmt),t=1):NaN, timecolumn(c,myTimeFmt)-t0)
# in case there are uncommented header lines skip them
SkipHeaderLines = 0
set multiplot layout 2,1
set format x "%Y\n%m-%d\n%H:%M" time
plot $Data u (timecolumn(1,myTimeFmt)):3 skip SkipHeaderLines w l ti "absolute time"
set format x "%tH:%tM" time
plot t=0 $Data u (Normalize(1)):3 skip SkipHeaderLines w l ti "relative time"
unset multiplot
### end of code
Result:

Filledcurves between different scaled y-axis

I have to "reinvent" a diagram like the following:
My problem is, the "filledcurves" option does not work correctly, if I use the different scaled y-axis.
set y2tics textcolor rgb "black"
set ytics nomirror
set yrange [0:80]
set y2range [0:180]
set key off
set grid dashtype 5 # auch dt ".-." möglich
plot "klima_flach.txt" using 1:3:4 with filledcurves x1,\
"" using 1:4 with lines axis x1y2,\
"" using 1:3:xtic(2) with lines axis x1y1
The used data is the following:
0 0 11 50
1 J 10 70
2 F 11 42
3 M 12 50
4 A 15 50
5 M 18 20
6 J 22 10
7 J 25 1
8 A 25 20
9 S 23 40
10 O 20 80
11 N 25 70
12 D 11 60
Any ideas, how I can get this problem solved?
By the way: A pattern as in the original diagram... possible or not?
#Wolfgang Höfer, the scaling between the axes in such type of Walter/Lieth-climate diagrams is 2. Hence, your y-range should be [0:90] and hence scaling factor 90./180.
Nevertheless, I assume #Christoph's answer solved your problem.
To your last question: a pattern as in your picture, i.e. a vertical hatch pattern? That's what I asked here (Hatch patterns in gnuplot) recently. Apparently, it's seems not possible in gnuplot.
Some time ago, I also "struggled" with climate diagrams, i.e. with filledcurves and even nonlinear axes. I would like to provide the code which I ended up. Maybe it will be useful to you or to others to draw such climate diagrams with gnuplot. If you are reading from a file, replace $DataIn with your filename. Suggestions and improvements are welcome.
# Walter/Lieth climate diagram with nonlinear axis
reset session
set encoding "utf8"
$DataIn <<EOD
# Mumbai/India, 18°54'N/72°49'E, 11 m
# No. Month Temperature Precipitation
1 January 23.9 3
2 February 23.9 3
3 March 26.1 3
4 April 28.1 2
5 May 29.7 18
6 June 28.9 485
7 July 27.2 617
8 August 27.0 340
9 September 27.0 264
10 October 28.1 64
11 November 27.2 13
12 December 25.6 3
EOD
# in order to be flexible for different input files
ColTemp = 3 # col# temperature
ColPrec = 4 # col# precipitation
# get location label from first commented row starting after '# '
set datafile commentschar "" # set the comment char to none
set datafile separator "\n" # data will be a full line
set table $Dummy # plot following data to a dummy table
# plots only first line 'every ::0::0' as string to the dummy table
# and assigns this line starting after the 3rd character to variable 'Location'
plot $DataIn u (Location = stringcolumn(1)[3:]) every ::0::0 with table
unset table # stop plotting to table
set datafile commentschar "#" # restore default commentschar
set datafile separator whitespace # restore default separator
set label 1 at graph 0.02,0.96 Location font ",10" # put label on graph
# set periodic boundaries, i.e. add lines of Dec and Jan again
# independent of the input format $DataIn, column1 of $Data will be the number of month
set datafile separator "\n"
set table $Data
plot $DataIn u (0):(stringcolumn(1)) every ::11::11 with table
plot $DataIn u ($0+1):(stringcolumn(1)) with table
plot $DataIn u (13):(stringcolumn(1)) every ::0::0 with table
unset table
set datafile separator whitespace
# print $Data
# settings for nonlinear scale
ScaleChangeAt = 100.
ScaleChangeFactor = 5.
f1(y) = (y<=ScaleChangeAt) ? y : ((y - ScaleChangeAt)/ScaleChangeFactor + ScaleChangeAt)
f2(y) = (y<=ScaleChangeAt) ? y : ((y - ScaleChangeAt)*ScaleChangeFactor + ScaleChangeAt)
f3(y) = f1(y)/2. # relation between axes y and y2; standard for Walter/Lieth climate diagrams
set nonlinear y2 via f1(y) inverse f2(y)
# settings for x-axis
set xrange[0.5:12.5]
set xtics 1 scale 0,1
set mxtics 2
set grid mxtics
# create months labels from local settings
do for [i=1:12] {
set xtics add (strftime("%b",strptime("%m",sprintf("%g",i))) i)
}
# settings for y- and y2-axes
stats [*:*] $DataIn u ColTemp:ColPrec nooutput
Round(m,n) = int(m/n)*n + sgn(m)*n
Ymin = STATS_min_x > 0 ? 0 : Round(STATS_min_x,10)
Ymax = 50
Y2min = Ymin < 0 ? f1(Ymin)*2 : 0
Y2max = Round(STATS_max_y,10**int(log(STATS_max_y)/log(10))) # round to next 10 or 100
# print Ymin, Ymax, Y2min, Y2max
# y-axis
set ylabel "Temperature / °C" tc rgb "red"
set yrange [Ymin:f3(Y2max)] # h(Y2max)]
set ytics 10 nomirror tc rgb "red"
# "manual" setting of ytics, up to 50°C
set ytics ("0" 0)
do for [i=Ymin:50:10] {
set ytics add (sprintf("%g",i) i)
}
# settings for y2-axis
set y2label "Precipitation / mm" tc rgb "blue"
set y2range [Y2min:Y2max]
# "manual" setting of y2tics
set y2tics nomirror tc rgb "blue"
set y2tics ("0" 0)
set grid y2tics
do for [i=20:ScaleChangeAt:20] {
set y2tics add (sprintf("%g",i) i)
}
do for [i=ScaleChangeAt:Y2max:20*ScaleChangeFactor] {
set y2tics add (sprintf("%g",i) i)
}
plot \
$Data u 1:ColTemp+1:(f3(column(ColPrec+1))) axis x1y1 w filledcurves above lc rgb "yellow" not,\
'' u 1:ColTemp+1:(f3(column(ColPrec+1))) axis x1y1 w filledcurves below fs pattern 4 fc rgb "blue" not,\
'' u 1:(f3(ScaleChangeAt)):(f3(column(ColPrec+1))) axis x1y1 w filledcurves below fs solid 1.0 fc rgb "blue" not,\
'' u 1:ColTemp+1 w l lw 2 lc rgb "red" not,\
'' u 1:ColPrec+1 axes x1y2 w l lw 2 lc rgb "blue" not
### end of code
which results in:
The filledcurves doesn't have an option for choosing different axes for the y-values in column two and three. But you are in the lucky situation, that you have fixed y-ranges. So you can define a scaling function for one of the columns:
set y2tics textcolor rgb "black"
set ytics nomirror
set yrange [0:80]
set y2range [0:180]
scale = 80.0/180.0
set key off
set grid dashtype 5 # auch dt ".-." möglich
plot "klima_flach.txt" using 1:3:(scale*$4) with filledcurves,\
"" using 1:4 with lines axis x1y2,\
"" using 1:3:xtic(2) with lines axis x1y1

Error with graphics gnuplot

I'm programming a graph with axes (y "%" and x "date") and when less than 7 records have not shown well
These are the files
data.csv:
20-04-2016 96.8 95.8 100
21-04-2016 97.07 97.99 100
22-04-2016 98.05 99.5 100
23-04-2016 98.64 97.88 100
24-04-2016 98.96 98.99 100
25-04-2016 98.68 98.11 100
graph
reset
set terminal png size 1200,500
#set xlabel "Time (days)"
set xdata time
set timefmt "%d-%m-%Y"
set ylabel "Total Petitions (%)"
set yrange [95:100]
set title "Akamai"
set key reverse Left outside
set grid
set style data linespoints
plot "datos.csv" using 1:2 title "One (%)", \
"" using 1:3 title "Two (%)", \
"" using 1:4 title "Ideal (%)"
He shows me this
As shown, the dates are repeated, and I want one date per record appears.
I also like to put the date
%d-%m\n%Y
This is solved if I put 7 or more records
And this happens if I put the full date but the description of the axis x
It could put the date vertically?
Thank you very much for your help
Use set format x;
set terminal pngcairo enhanced color dashed \
rounded size 1200,500
set title "Akamai"
set xlabel "date"
set xdata time
set timefmt "%d-%m-%Y"
set format x "%d/%m\n%Y"
set xtics 86400
set nomxtics
set ylabel "Total Petitions (%)"
set yrange [95:100]
set style data linespoints
set grid
set key right bottom
set output 'user1847844.png'
plot "datos.csv" using 1:2 title "One (%)", \
"" using 1:3 title "Two (%)", \
"" using 1:4 title "Ideal (%)"
Internally, gnuplot converts datetimes to seconds. So the line set xtics 86400 basically says to make one tick per day since one day has 24*60*60 = 86400 seconds. The set nomxtics tells gnuplot to not show minor tick marks on the x-axis.
This gives:
If you want to rotate the label, use:
set format x "%d/%m/%Y"
set xtics rotate 86400
That should format the dates as a single line and rotate the labels by 90 degrees.
Update:
Note that I'm using the pngcairo terminal. This is based on the cairo graphics library. This is designed to give consistent output on all output media. But gnuplot must be compiled with cairo support for this to work.
In general, I prefer the pdfcairo terminal. This produces PDF output which can be scaled without giving pixellation effects.
Update 2:
To get consistency in my graphs, I've defined certain styles in my gnuplotrc, which I use in my graphs. The contents of my gnuplotrc are shown below.
set encoding utf8
# See https://github.com/Gnuplotting/gnuplot-palettes
# Line styles (colorbrewer Set1)
set style line 1 lc rgb '#E41A1C' pt 1 ps 1 lt 1 lw 2 # red
set style line 2 lc rgb '#377EB8' pt 6 ps 1 lt 1 lw 2 # blue
set style line 3 lc rgb '#4DAF4A' pt 2 ps 1 lt 1 lw 2 # green
set style line 4 lc rgb '#984EA3' pt 3 ps 1 lt 1 lw 2 # purple
set style line 5 lc rgb '#FF7F00' pt 4 ps 1 lt 1 lw 2 # orange
set style line 6 lc rgb '#FFFF33' pt 5 ps 1 lt 1 lw 2 # yellow
set style line 7 lc rgb '#A65628' pt 7 ps 1 lt 1 lw 2 # brown
set style line 8 lc rgb '#F781BF' pt 8 ps 1 lt 1 lw 2 # pink
# Palette
set palette maxcolors 8
set palette defined ( 0 '#E41A1C', 1 '#377EB8', 2 '#4DAF4A', 3 '#984EA3',\
4 '#FF7F00', 5 '#FFFF33', 6 '#A65628', 7 '#F781BF' )
# Standard border
set style line 11 lc rgb '#808080' lt 1 lw 3
set border 0 back ls 11
set tics out nomirror
# Standard grid
set style line 12 lc rgb '#808080' lt 0 lw 1
set grid back ls 12
unset grid
And I use a pdfcairo terminal as standard, like this.
set terminal pdfcairo enhanced color dashed font "Alegreya, 14" \
rounded size 16 cm, 9.6 cm
The font used is an example. For graphs in reports I tend to use the same font as the body text.
The plot command uses the defined styles.
plot "user1847844.d" using 1:2 ls 1 title "One (%)", \
"" using 1:3 ls 2 title "Two (%)", \
"" using 1:4 ls 3 title "Ideal (%)"
This produces a figure looking like this.
Note: Since StackOverflow doesn't accept PDF images, I converted it to a PNG using ImageMagick.
convert -density 300 <input.pdf> -quality 100 -sharpen 0x1.0 <output.png>

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)

Resources