Gnuplot: Find y value for given x value - gnuplot

I have a set of data points I plot with gnuplot. Know I calculate a value for x and want to find the corresponding y value within the data.
Does anybody know how to manage this with gnuplot?

I found kind of a solution here on StackOverflow
Basically you have to invert the function you use to calculate y (or x in the case of the link) and then, thanks to the sprintf function, you gate the corresponding value.
NOTE
Have a look also here if you did not find the solution at the first link!

Although this is a rather old question, the following solution for interpolation might still be of interest to others.
The code can be simplified depending on whether you are just interested in nearest datapoint or in the interpolated value or whether you want to plot the value as point and/or label.
Code:
### interpolate between datapoints
reset session
$Data <<EOD
0 0
0.30 0.20
0.60 0.40
0.80 0.80
1 1
EOD
$Data2 <<EOD
1 0
0.80 0.15
0.50 0.30
0.50 0.50
0.30 0.80
0 1
EOD
InterpolY(x,x0,x1,y0,y1) = (x-x0)*(x-x1)<0 || x1-x0==0 ? x1-x0==0 ? (y0+y1)/2 : (y1-y0)/(x1-x0)*(x-x0) +y0 : NaN
set key noautotitle
array Point[1] # dummy array for plotting a single datapoint
# x-value for interpolation is defined in xp
plot x1=y1=yp=(xp=0.7,NaN) $Data u (x0=x1,x1=$1):(y0=y1,y1=$2, yp==yp ? NaN : yp=InterpolY(xp,x0,x1,y0,y1),y1) w lp pt 7, \
Point u (xp):(yp) w p pt 7 lc "red", \
Point u (xp):(yp):(sprintf("(%.2g|%.2g)",xp,yp)) w labels right offset -1,0 lc "red", \
x1=y1=yp=(xp=0.5,NaN) $Data2 u (x0=x1,x1=$1):(y0=y1,y1=$2, yp==yp ? NaN : yp=InterpolY(xp,x0,x1,y0,y1),y1) w lp pt 7, \
Point u (xp):(yp) w p pt 7 lc "green", \
Point u (xp):(yp):(sprintf("(%.2g|%.2g)",xp,yp)) w labels right offset -1,0 lc "green", \
### end of code
Result: (interpolated values at x=0.7 and x=0.5)

Related

Marking zero values with a red line in gnuplot

I have a .csv datafile that outputs values from 0 to 500 i.e
25.2
2.82
2.05
2.13
2.42
2.17
2.00
0
0
0
0
3.33
3.41
3.26
3.30
0
27.8
and plot it using
plot 'out.csv' using 1 with lines
From that file I would like any 0 values to be marked on the graph with a single red line
Depending on what exactly you are looking for, the following would be my suggestion from what I understood from your question.
Plot the data again with impulses and filtered with the ternary operator (check help ternary).
Script:
### plot with ternary operator
reset session
# create some random test data
set samples 300
set table $Data
plot '+' u (rand(0)<0.97 ? (rand(0)**20)*500+1 : 0) w table
unset table
set key out
plot $Data u 1 w l lc 1 ti "data", \
'' u ($1==0 ? 500 : NaN) w impulses lc "red" ti "0 value"
### end of script
Result:

Gnuplot pull function range from a file for each row

I am trying to display a function which is a linear function f(x)=a*x+b with different coefficients in each range. My file (output.txt) looks like this but without the header:
a b range
-0.0645991 0.439102 0 0.25
-0.230142 0.480488 0.25 0.5
-0.438559 0.584697 0.5 0.75
-0.66962 0.757993 0.75 1
-0.898603 0.986975 1 1.25
-1.09846 1.2368 1.25 1.5
-1.24232 1.45259 1.5 1.75
-1.30601 1.56404 1.75 2
I have tried these lines but none of them worked:
plot "output.txt" using 1:2 with lines, set xrange using 3:4
plot "output.txt" [($3):($4)] ($1)*x+($2)
plot "output.txt" $3<=x<=$4 ? ($1)*x+($2) : 0
It's my first time using Gnuplot and I couldn't find any example of pulling range from a file.
Welcome to StackOverflow! You can simply use the plotting style with vectors.
Check help vectors.
The plotting command looks complicated but it is straightforward.
The four values you need are: x, y, delta x, and delta y, i.e.
in your example
x: 3rd column
y: f(x,a,b) where x is 3rd column and a and b are from 1st and 2nd columns, respectively.
dx: 4th column less 3rd column
dy: f(x,a,b) where x is from 4th column less f(x,a,b) where x is from 3rd column and a,b as above.
You can skip the arrow heads if you specify nohead in the plot command. Check help arrow.
Code:
### plotting style with vectors
reset session
$Data <<EOD
a b range
-0.0645991 0.439102 0 0.25
-0.230142 0.480488 0.25 0.5
-0.438559 0.584697 0.5 0.75
-0.66962 0.757993 0.75 1
-0.898603 0.986975 1 1.25
-1.09846 1.2368 1.25 1.5
-1.24232 1.45259 1.5 1.75
-1.30601 1.56404 1.75 2
EOD
f(x,a,b) = a*x + b
plot $Data u 3:(f($3,$1,$2)):($4-$3):(f($4,$1,$2)-f($3,$1,$2)) w vectors lc "red" head title "my function"
### end of code
Result:

Gnuplot labels in column with different shape

I have a little problem. I try create gnu plot with many lines or shapes but header is in last column . Example data in file
18:40:03 0.00 K/s 3.65 K/s 0.00 % 0.25 % AA
18:40:03 0.00 K/s 69.44 K/s 0.00 % 0.05 % BB
18:40:03 0.40 K/s 0.00 K/s 0.00 % 0.03 % AA
my gnuplot code
set xlabel "Date\nTime"
set timefmt "%H:%M:%S"
set format x "%H:%M:%S"
set xdata time
set autoscale xy
set term png
set output "io.png"
plot 'io.log' using 0:8 title 'title', \
'' using 0:8:10 with labels offset 0,char 1
~
But I want create plot where this same type (AA) have this same color or shape and different from BB CC .....
in
A rather old question with no feedback of the OP, but maybe still of interest to others.
As I understand, you are basically looking for a lookup table with string input.
You can define your keys, pointtypes, colors in strings and "mis-use" the function sum to define a lookup function.
Check help sum, help word, help variable.
Script:
### lookup tables with string input for different point types and colors
reset session
$Data <<EOD
1 9 AA
2 8 BB
3 6 CC
4 8 AA
5 6 BB
6 4 DD
7 2 CC
8 4 AA
EOD
myLabels = "AA BB CC DD"
myPts = "5 7 9 11" # pointtypes
myColors = "0xff0000 0x00ff00 0x0000ff 0xff00ff"
myPt(col) = int(word(myPts,Lookup(strcol(col))))
myColor(col) = int(word(myColors,int(Lookup(strcol(col)))))
Lookup(s) = int(sum [_i=1:words(myLabels)] (word(myLabels,_i) eq s ? _i : 0))
set key noautotitle
set grid x,y
set offset 0.5,0.5,0.5,0.5
plot $Data u 1:2:3:(myColor(3)) w labels tc rgb var font ",12" offset 0,1, \
'' u 1:2:(myPt(3)):(myColor(3)) w p ps 2 pt var lc rgb var
### end of script
Result:
In order to simplify things, let's assume that the input data is stored in a file test.dat and it has following form:
1 2 AA
2 4 BB
3 6 AA
Then there are several possibilities to distinguish the style based on the value of the label in the third column.
One could for example invoke external processing to filter out records of the "special type" (label "AA" in your case) and specify a particular style:
unset key
set xr [0:4]
set yr [0:10]
plot \
'< gawk ''$3=="AA"'' test.dat' using 1:2 w p pt 1, \
'' u 1:2:3 w labels offset 0,char 1 tc rgb 'red', \
'< gawk ''$3!="AA"'' test.dat' using 1:2 w p pt 2, \
'' u 1:2:3 w labels offset 0,char 1 tc rgb 'blue'
This produces:
Alternatively, in order to distinguish just the style of the labels, one could filter individual cases directly in Gnuplot as:
set terminal png enhanced
set output 'fig2.png'
unset key
set xr [0:4]
set yr [0:10]
checkLabel(label)=(label eq 'AA')
filter(label, val, flag)=(((flag && checkLabel(label))||(!flag && !checkLabel(label)))?val:1/0)
plot \
'test.dat' using 1:2 w p , \
'' using 1:(filter(stringcolumn(3), $2, 1)):3 w labels offset 0,char 1 tc rgb 'red', \
'' using 1:(filter(stringcolumn(3), $2, 0)):3 w labels offset 0,char 1 tc rgb 'blue'
which yields:

Gnuplot: plotting points with variable point types

I have x,y values for points in the first 2 colums and a number that indicates the point type (symbol) in the 3. column, in one data file. How do I plot data points with different symbols?
Unfortunately, there isn't a way (AFAIK) to automatically set the point of the plot from a column value using vanilla GNUPLOT.
However, there is a way to get around that by setting a linestyle for each data series, and then plotting the values based on that defined style:
set style line 1 lc rgb 'red' pt 7 #Circle
set style line 2 lc rgb 'blue' pt 5 #Square
Remember that the number after pt is the point-type.
Then, all you have to do is plot (assuming that the data in "data.txt" is ordered ColX ColY Col3):
plot "data.txt" using 1:2 title 'Y Axis' with points ls 1, \
"data.txt" using 1:3 title 'Y Axis' with points ls 2
Try it here using this data (in the section titled "Data" - also note that column 3 "Symbol" is noted used, it's mainly there for illustrative purposes):
# This file is called force.dat
# Force-Deflection data for a beam and a bar
# Deflection Col-Force Symbol
0.000 0 5
0.001 104 5
0.002 202 7
0.003 298 7
And in the Plot Script Heading:
set key inside bottom right
set xlabel 'Deflection (m)'
set ylabel 'Force (kN)'
set title 'Some Data'
set style line 1 lc rgb 'red' pt 7
set style line 2 lc rgb 'blue' pt 5
plot "data.txt" using 1:2 title 'Col-Force' with points ls 1, \
"data.txt" using 1:3 title 'Beam-Force' with points ls 2
The one caveat is of course that you have have to reconfigure your data input source.
REFERENCES:
http://www.gnuplotting.org/plotting-single-points/
http://www.gnuplotting.org/plotting-data/
Here is a possible solution (which is a simple extrapolation from gnuplot conditional plotting with if), that works as long as you don't have tens of different symbols to handle.
Suppose I want to plot 2D points in a coordinate system. I have only two symbols, that I arbitrarily represented with a 0 and a 1 in the last column of my data file :
0 -0.29450470209121704 1.2279523611068726 1
1 -0.4006965458393097 1.0025811195373535 0
2 -0.7109975814819336 0.9022682905197144 1
3 -0.8540692329406738 1.0190201997756958 1
4 -0.5559651851654053 0.7677079439163208 0
5 -1.1831613779067993 1.5692367553710938 0
6 -0.24254602193832397 0.8055955171585083 0
7 -0.3412654995918274 0.6301406025886536 0
8 -0.25005266070365906 0.7788659334182739 1
9 -0.16853423416614532 0.09659398347139359 1
10 0.169997438788414 0.3473801910877228 0
11 -0.5252010226249695 -0.1398928463459015 0
12 -0.17566296458244324 0.09505800902843475 1
To achieve what I want, I just plot my file using conditionals. Using an undefined value like 1/0 results in no plotting of the given point:
# Set styles
REG_PTS = 'pointtype 7 pointsize 1.5 linecolor rgb "purple"'
NET_PTS = 'pointtype 4 pointsize 1.5 linecolor rgb "blue"'
set grid
# Plot each category with its own style
plot "data_file" u 2:($4 == 0 ? $3 : 1/0) title "regular" #REG_PTS, \
"data_file" u 2:($4 == 1 ? $3 : 1/0) title "network" #NET_PTS
Here is the result :
Hope this helps
Variable pointype (pt variable) was introduced (I guess) not until gnuplot 5.2.0 (Sept 2017) (check help points).
Just in retrospective, another (awkward) solution would be the following for those who are still using such early versions.
Data:
1 1.0 4 # empty square
2 2.0 5 # filled square
3 3.0 6 # empty circle
4 4.0 7 # filled circle
5 5.0 8 # empty triangle up
6 6.0 9 # filled triangle down
7 7.0 15 # filled pentagon (cross in gnuplot 4.6 to 5.0)
Script: (works from gnuplot>=4.6.0, March 2012; but not necessary since 5.2.0)
### variable pointtype for gnuplot>=4.6
reset
FILE = 'SO23707979.dat'
set key noautotitle
set offsets 1,1,1,1
set pointsize 4
stats FILE u 0 nooutput
N = STATS_records # get the number of rows
p0=x1=y1=NaN
plot for [n=0:N-1 ] FILE u (x0=x1, x1=$1, x0):(y0=y1, y1=$2, y0):(p0=$3) \
every ::n::n w p pt p0 lc rgb "red", \
FILE u 1:2 every ::N-1::N-1 w p pt p0 lc rgb "red"
### end of script
Result:

merging multiple data files to plot in a single graph

extending the question I had asked earlier which can be found here, plotting multiple (x,y) co-ordinates in a single curve with gnuplot. I am trying to plot the bezier curves in gnuplot using 2 different files. Each (x,y) from the first file forms a bezier curve passing through the points from the second file. The first file has the co-ordinates like the following:
x y
0.0 0.5
0.12 0.1
0.16 0.4
0.2 0.35
0.31 0.8
0.34 0.6
0.38 1.0
0.46 0.2
0.51 0.7
0.69 0.9
The second file has the co-ordinates as the following:
x y
0.00 0.7
0.04 0.74073082208
0.08 0.85926917792
0.12 0.9
0.16 0.9
0.2 0.9
0.24 0.749720623086
0.28 0.874229601255
0.32 0.74073082208
0.36 0.8
0.4 0.721178508605
0.44 0.878821491395
0.48 0.761772990545
0.52 0.700774803388
0.56 0.723771273415
0.6 0.789508073675
0.64 0.864014272269
0.68 0.896743348931
Now, how do I merge these two files to plot a single graph. These two files don't have the same number of rows, but I guess that doesn't matter. The first curve would be between (x1,y1) and (x2,y2) from the first file which would continue till (x10,y10). In the curve between (x1,y1) and (x2,y2); the points (x1,y1), (x2,y2) and (x3,y3) from the second file lie.
I followed this link http://t16web.lanl.gov/Kawano/gnuplot/datafile3-e.html to sort and concatenate the two files but got some weird lines which is totally wrong. These values should actually plot Bezier curves but not getting the graph. I have written the following gnuplot script to plot the concatenated data:
set term x11 persist
set title "Animation curves"
set xlabel "Time (secs.)"
set ylabel "Parameter"
set x2label "Phoneme1" offset -35
set pointsize 2
set key off
set style line 2 lt 0 lc 1 lw 2
set xrange [0.0:1.0]
set yrange [0.0:1.3]
plot [0.0:0.8] "< cat -n file1.dat" u 1:2 smooth csplines ls 1, "" u 1:(0.0):(0):(1.3) w vectors nohead ls 2, "" u ($1+0.005):(1):(sprintf("P %d", $0)) w labels, \
"file1.dat" u 1:2 with points, \
"file2.dat" u 1:2 with points, \
I got the following error:
plot "< cat -n file1.dat" u 1:2 smooth csplines ls 1, "" u 1:(0.0):(0):(1.3) w vectors nohead ls 2, "" u ($1+0.005):(1):(sprintf("P %d", $0)) w labels, "file1.dat" u 1:2 with points, "file2.dat" u 1:2 with points,
^
"plot.gp", line 21: Cannot smooth: no data within fixed xrange!
The script below works on my machine. Maybe this is even what you are looking for...
set term x11 persist
set title "Animation curves"
set xlabel "Time (secs.)"
set ylabel "Parameter"
set x2label "Phoneme1" offset -35
set pointsize 2
set key off
set style line 2 lt 0 lc 1 lw 2
set xrange [0.0:1.0]
set yrange [0.0:1.3]
plot [0.0:0.8] "< cat file1.dat file2.dat | sort -n" u 1:2 smooth csplines ls 1, \
"" u 1:(0):(0):2 w vectors nohead ls 2, \
"" u ($1 + 0.005):(1):(sprintf("P %d", $0)) w labels, \
"file1.dat" u 1:2 w p, \
"file2.dat" u 1:2 w p

Resources